@@ -79,6 +79,11 @@ | |||
<version>1.2.9</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-validation</artifactId> | |||
</dependency> | |||
</dependencies> | |||
<!-- 构建环境变量 --> | |||
@@ -117,6 +122,31 @@ | |||
</profiles> | |||
<build> | |||
<resources> | |||
<resource> | |||
<directory>src/main/resources</directory> | |||
<filtering>true</filtering> | |||
</resource> | |||
<resource> | |||
<directory>src/main/java</directory> | |||
<includes> | |||
<include>**/*.*</include> | |||
</includes> | |||
<excludes> | |||
<exclude>**/*.java</exclude> | |||
</excludes> | |||
</resource> | |||
<resource> | |||
<directory>src/main/resources</directory> | |||
<filtering>true</filtering> | |||
<targetPath>WEB-INF/classes</targetPath> | |||
<includes> | |||
<include>application-${package.environment}.yml</include> | |||
</includes> | |||
</resource> | |||
</resources> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.springframework.boot</groupId> |
@@ -13,6 +13,8 @@ import org.springframework.security.config.annotation.web.configurers.Expression | |||
import org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization.OAuth2AuthorizationServerConfigurer; | |||
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer; | |||
import org.springframework.security.core.userdetails.UserDetailsService; | |||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |||
import org.springframework.security.crypto.password.PasswordEncoder; | |||
import org.springframework.security.oauth2.core.oidc.OidcUserInfo; | |||
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings; | |||
import org.springframework.security.oauth2.server.authorization.oidc.authentication.OidcUserInfoAuthenticationContext; | |||
@@ -77,9 +79,11 @@ public class SecurityConfig { | |||
@Bean | |||
@Order(2) | |||
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception { | |||
http | |||
http.csrf().disable() | |||
.authorizeHttpRequests((authorize) -> authorize | |||
.antMatchers("/getHealth").permitAll() | |||
.antMatchers("/user/create").permitAll() | |||
.anyRequest().authenticated() | |||
) | |||
// Form login handles the redirect to the login page from the | |||
@@ -106,4 +110,9 @@ public class SecurityConfig { | |||
return ProviderSettings.builder().build(); | |||
} | |||
// @Bean | |||
// public PasswordEncoder passwordEncoder() { | |||
// return new BCryptPasswordEncoder(); | |||
// } | |||
} |
@@ -4,8 +4,6 @@ import com.tuoheng.model.param.CreateUserDto; | |||
import com.tuoheng.service.UserSevice; | |||
import com.tuoheng.until.JsonResult; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Controller; | |||
import org.springframework.validation.annotation.Validated; | |||
import org.springframework.web.bind.annotation.*; | |||
/** | |||
@@ -21,7 +19,7 @@ public class UserController { | |||
private UserSevice userSevice; | |||
@PostMapping("/create") | |||
public JsonResult createUser(@RequestBody @Validated CreateUserDto createUserDto){ | |||
public JsonResult createUser(@RequestBody CreateUserDto createUserDto){ | |||
return userSevice.createUser(createUserDto); | |||
} | |||
@@ -1,5 +1,6 @@ | |||
package com.tuoheng.mapper; | |||
import com.tuoheng.model.po.UserPo; | |||
import org.apache.ibatis.annotations.Mapper; | |||
/** | |||
@@ -10,6 +11,6 @@ import org.apache.ibatis.annotations.Mapper; | |||
@Mapper | |||
public interface UserMapper { | |||
int insertUser(); | |||
int insertUser(UserPo userPo); | |||
} |
@@ -2,6 +2,7 @@ package com.tuoheng.model.param; | |||
import lombok.Data; | |||
import javax.validation.constraints.NotEmpty; | |||
import java.util.List; | |||
/** | |||
@@ -12,8 +13,10 @@ import java.util.List; | |||
@Data | |||
public class CreateUserDto { | |||
@NotEmpty(message = "username can not be empty!") | |||
private String username; | |||
@NotEmpty(message = "password can not be empty!") | |||
private String password; | |||
private List<String> roles; |
@@ -1,6 +1,7 @@ | |||
package com.tuoheng.model.po; | |||
import lombok.Data; | |||
import lombok.experimental.Accessors; | |||
/** | |||
* @author chenjiandong | |||
@@ -8,6 +9,7 @@ import lombok.Data; | |||
* @date 2022/10/8 12:07 | |||
*/ | |||
@Data | |||
@Accessors(chain = true) | |||
public class UserPo { | |||
private Integer id; |
@@ -1,6 +1,8 @@ | |||
package com.tuoheng.service.impl; | |||
import com.tuoheng.mapper.UserMapper; | |||
import com.tuoheng.model.param.CreateUserDto; | |||
import com.tuoheng.model.po.UserPo; | |||
import com.tuoheng.service.UserSevice; | |||
import com.tuoheng.until.JsonResult; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
@@ -18,14 +20,20 @@ import org.springframework.stereotype.Service; | |||
@Service | |||
public class UserServiceImpl implements UserSevice { | |||
@Autowired | |||
private UserMapper userMapper; | |||
public JsonResult createUser(CreateUserDto createUserDto){ | |||
// UserDetails userDetails = User.builder().passwordEncoder(s -> "{bcrypt}" + new BCryptPasswordEncoder().encode(s)) | |||
// .username("admin") | |||
// .password("123456") | |||
// .roles("ADMIN") | |||
// .build(); | |||
// UserDetailsManager userDetailsManager = new UserDetailsManager(); | |||
// userDetailsManager.createUser(userDetails); | |||
UserPo userPo = new UserPo() | |||
.setUsername(createUserDto.getUsername()) | |||
.setPassword("{bcrypt}" + new BCryptPasswordEncoder().encode(createUserDto.getPassword())); | |||
userMapper.insertUser(userPo); | |||
return JsonResult.success(); | |||
} | |||
@@ -1,5 +1,6 @@ | |||
package com.tuoheng.until; | |||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | |||
import org.springframework.security.oauth2.core.ClientAuthenticationMethod; | |||
import org.springframework.security.oauth2.core.OAuth2TokenFormat; | |||
@@ -24,6 +25,7 @@ public class RegisteredClientUtil { | |||
.clientId("tuoheng-hhz") | |||
.clientName("tuoheng-hhz-client") | |||
.clientSecret("{noop}" + CryptoUtil.genAesSecret()) | |||
//.clientSecret("{bcrypt}" + new BCryptPasswordEncoder().encode("secret")) | |||
.clientAuthenticationMethods(s -> { | |||
s.add(ClientAuthenticationMethod.CLIENT_SECRET_POST); | |||
s.add(ClientAuthenticationMethod.CLIENT_SECRET_BASIC); |
@@ -34,4 +34,4 @@ spring: | |||
validationQuery: SELECT 1 FROM DUAL | |||
testWhileIdle: true | |||
testOnBorrow: false | |||
testOnReturn: false | |||
testOnReturn: false |
@@ -3,4 +3,7 @@ server: | |||
spring: | |||
profiles: | |||
active: @package.environment@ | |||
active: @package.environment@ | |||
mybatis: | |||
mapper-locations: classpath*:mapper/*Mapper.xml |
@@ -3,8 +3,8 @@ | |||
<mapper namespace="com.tuoheng.mapper.UserMapper"> | |||
<insert id="insertUser" parameterType="com.tuoheng.model.po.UserPo"> | |||
insert into users (username, password, enabled, client_id) | |||
values (#{aName,jdbcType=VARCHAR}, #{aPass,jdbcType=VARCHAR}) | |||
insert into users (username, password) | |||
values (#{username}, #{password}) | |||
</insert> | |||
</mapper> |