@@ -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 |
@@ -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); | |||
} |
@@ -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; | |||
} |
@@ -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; | |||
} |
@@ -20,6 +20,4 @@ public class UserPo { | |||
private Integer enabled; | |||
private String clientId; | |||
} |
@@ -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()); | |||
} | |||
} |
@@ -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) |
@@ -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> |
@@ -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> |
@@ -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() { | |||
} | |||
} |
@@ -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); | |||
} | |||
} |