瀏覽代碼

first commit

tags/V2.2.0
chenjiandong 2 年之前
父節點
當前提交
bcc45883e5
共有 11 個檔案被更改,包括 69 行新增63 行删除
  1. +1
    -1
      Spring_Authorization_Server/src/main/java/com/tuoheng/config/SecurityConfig.java
  2. +16
    -0
      Spring_Authorization_Server/src/main/java/com/tuoheng/mapper/AuthoritiesMapper.java
  3. +2
    -0
      Spring_Authorization_Server/src/main/java/com/tuoheng/model/param/CreateUserDto.java
  4. +23
    -0
      Spring_Authorization_Server/src/main/java/com/tuoheng/model/po/AuthoritiesPo.java
  5. +0
    -2
      Spring_Authorization_Server/src/main/java/com/tuoheng/model/po/UserPo.java
  6. +16
    -7
      Spring_Authorization_Server/src/main/java/com/tuoheng/service/impl/UserServiceImpl.java
  7. +0
    -1
      Spring_Authorization_Server/src/main/java/com/tuoheng/until/RegisteredClientUtil.java
  8. +10
    -0
      Spring_Authorization_Server/src/main/resources/mapper/AuthoritiesMapper.xml
  9. +1
    -1
      Spring_Authorization_Server/src/main/resources/mapper/UserMapper.xml
  10. +0
    -13
      Spring_Authorization_Server/src/test/java/com/tuoheng/SpringAuthorizationServerApplicationTests.java
  11. +0
    -38
      Spring_Authorization_Server/src/test/java/com/tuoheng/config/ApplicationTests.java

+ 1
- 1
Spring_Authorization_Server/src/main/java/com/tuoheng/config/SecurityConfig.java 查看文件

@@ -80,7 +80,7 @@ public class SecurityConfig {
http.csrf().disable()
.authorizeHttpRequests((authorize) -> authorize
.antMatchers("/getHealth").permitAll()
//.antMatchers("/user/create").permitAll()
.antMatchers("/user/create").permitAll()
.anyRequest().authenticated()
)
// Form login handles the redirect to the login page from the

+ 16
- 0
Spring_Authorization_Server/src/main/java/com/tuoheng/mapper/AuthoritiesMapper.java 查看文件

@@ -0,0 +1,16 @@
package com.tuoheng.mapper;

import com.tuoheng.model.po.AuthoritiesPo;
import org.apache.ibatis.annotations.Mapper;

/**
* @author chenjiandong
* @description: TODO
* @date 2022/10/9 10:39
*/
@Mapper
public interface AuthoritiesMapper {

int insertAuthorities(AuthoritiesPo authoritiesPo);

}

+ 2
- 0
Spring_Authorization_Server/src/main/java/com/tuoheng/model/param/CreateUserDto.java 查看文件

@@ -3,6 +3,7 @@ package com.tuoheng.model.param;
import lombok.Data;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
@@ -19,6 +20,7 @@ public class CreateUserDto {
@NotEmpty(message = "password can not be empty!")
private String password;

@NotNull(message = "roles can not be empty!")
private List<String> roles;

}

+ 23
- 0
Spring_Authorization_Server/src/main/java/com/tuoheng/model/po/AuthoritiesPo.java 查看文件

@@ -0,0 +1,23 @@
package com.tuoheng.model.po;

import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author chenjiandong
* @description: TODO
* @date 2022/10/9 10:20
*/
@Data
@Accessors(chain = true)
public class AuthoritiesPo {

private Integer id;

private Integer userId;

private String username;

private String authority;

}

+ 0
- 2
Spring_Authorization_Server/src/main/java/com/tuoheng/model/po/UserPo.java 查看文件

@@ -20,6 +20,4 @@ public class UserPo {

private Integer enabled;

private String clientId;

}

+ 16
- 7
Spring_Authorization_Server/src/main/java/com/tuoheng/service/impl/UserServiceImpl.java 查看文件

@@ -1,7 +1,10 @@
package com.tuoheng.service.impl;

import com.sun.deploy.util.StringUtils;
import com.tuoheng.mapper.AuthoritiesMapper;
import com.tuoheng.mapper.UserMapper;
import com.tuoheng.model.param.CreateUserDto;
import com.tuoheng.model.po.AuthoritiesPo;
import com.tuoheng.model.po.UserPo;
import com.tuoheng.service.UserSevice;
import com.tuoheng.until.JsonResult;
@@ -11,6 +14,7 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author chenjiandong
@@ -23,18 +27,23 @@ public class UserServiceImpl implements UserSevice {
@Autowired
private UserMapper userMapper;

@Autowired
private AuthoritiesMapper authoritiesMapper;

@Transactional(rollbackFor = Exception.class)
public JsonResult createUser(CreateUserDto createUserDto){
// UserDetails userDetails = User.builder().passwordEncoder(s -> "{bcrypt}" + new BCryptPasswordEncoder().encode(s))
// .username("admin")
// .password("123456")
// .roles("ADMIN")
// .build();
// userDetailsManager.createUser(userDetails);
UserPo userPo = new UserPo()
.setUsername(createUserDto.getUsername())
.setPassword("{bcrypt}" + new BCryptPasswordEncoder().encode(createUserDto.getPassword()));
userMapper.insertUser(userPo);
return JsonResult.success();
String roles = StringUtils.join(createUserDto.getRoles(),",");

AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())
.setUsername(createUserDto.getUsername())
.setAuthority(roles);
authoritiesMapper.insertAuthorities(authoritiesPo);
return JsonResult.success(userPo.getId());
}

}

+ 0
- 1
Spring_Authorization_Server/src/main/java/com/tuoheng/until/RegisteredClientUtil.java 查看文件

@@ -35,7 +35,6 @@ public class RegisteredClientUtil {
.redirectUri("http://www.baidu.com")
.scope(OidcScopes.OPENID)
.scope(OidcScopes.PROFILE)
.scope(OidcScopes.EMAIL)
.clientSettings(ClientSettings.builder()
.requireAuthorizationConsent(true)
.requireProofKey(false)

+ 10
- 0
Spring_Authorization_Server/src/main/resources/mapper/AuthoritiesMapper.xml 查看文件

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tuoheng.mapper.AuthoritiesMapper">

<insert id="insertAuthorities" parameterType="com.tuoheng.model.po.AuthoritiesPo">
insert into authorities (user_id, username, authority)
values (#{userId}, #{username}, #{authority})
</insert>

</mapper>

+ 1
- 1
Spring_Authorization_Server/src/main/resources/mapper/UserMapper.xml 查看文件

@@ -2,7 +2,7 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tuoheng.mapper.UserMapper">

<insert id="insertUser" parameterType="com.tuoheng.model.po.UserPo">
<insert id="insertUser" parameterType="com.tuoheng.model.po.UserPo" keyProperty="id" useGeneratedKeys="true">
insert into users (username, password)
values (#{username}, #{password})
</insert>

+ 0
- 13
Spring_Authorization_Server/src/test/java/com/tuoheng/SpringAuthorizationServerApplicationTests.java 查看文件

@@ -1,13 +0,0 @@
package com.tuoheng;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringAuthorizationServerApplicationTests {

@Test
void contextLoads() {
}

}

+ 0
- 38
Spring_Authorization_Server/src/test/java/com/tuoheng/config/ApplicationTests.java 查看文件

@@ -1,38 +0,0 @@
package com.tuoheng.config;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.UserDetailsManager;

/**
* @author chenjiandong
* @description: TODO
* @date 2022/10/8 11:05
*/
@SpringBootTest
class ApplicationTests {

/**
* 初始化客户端信息
*/
@Autowired
private UserDetailsManager userDetailsManager;

/**
* 创建用户信息
*/
@Test
void testSaveUser() {
UserDetails userDetails = User.builder().passwordEncoder(s -> "{bcrypt}" + new BCryptPasswordEncoder().encode(s))
.username("admin")
.password("123456")
.roles("ADMIN")
.build();
userDetailsManager.createUser(userDetails);
}

}

Loading…
取消
儲存