From 8087db0c34c4563aea0a67512717873d2434b651 Mon Sep 17 00:00:00 2001 From: "wangiegie@gmail.com" Date: Mon, 4 Dec 2017 22:35:34 +0800 Subject: [PATCH] =?UTF-8?q?end:=20=20=20=20=E5=AF=86=E7=A0=81=E4=BD=BF?= =?UTF-8?q?=E7=94=A8BCryptPasswordEncoder=E6=95=A3=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/github/pig/admin/controller/UserController.java | 7 ++++--- .../java/com/github/pig/auth/PigAuthServerApplication.java | 1 - .../com/github/pig/auth/config/PigAuthorizationConfig.java | 7 +++++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java index 9601a299..d9c33c82 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java @@ -13,11 +13,10 @@ import com.github.pig.common.constant.CommonConstant; import com.github.pig.common.util.UserUtils; import com.github.pig.common.vo.UserVo; import com.github.pig.common.web.BaseController; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.web.bind.annotation.*; import java.util.Date; @@ -29,6 +28,7 @@ import java.util.Date; @RestController @RequestMapping("/user") public class UserController extends BaseController { + private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder(); @Autowired private UserService userService; @Autowired @@ -94,6 +94,7 @@ public class UserController extends BaseController { SysUser sysUser = new SysUser(); BeanUtils.copyProperties(userDto, sysUser); sysUser.setDelFlag(CommonConstant.STATUS_NORMAL); + sysUser.setPassword(ENCODER.encode(userDto.getPassword())); userService.insert(sysUser); SysUserRole userRole = new SysUserRole(); userRole.setUserId(sysUser.getUserId()); diff --git a/pig-auth-service/src/main/java/com/github/pig/auth/PigAuthServerApplication.java b/pig-auth-service/src/main/java/com/github/pig/auth/PigAuthServerApplication.java index 1a62331e..c03daf2a 100644 --- a/pig-auth-service/src/main/java/com/github/pig/auth/PigAuthServerApplication.java +++ b/pig-auth-service/src/main/java/com/github/pig/auth/PigAuthServerApplication.java @@ -4,7 +4,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; -import org.springframework.context.annotation.ComponentScan; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; /** diff --git a/pig-auth-service/src/main/java/com/github/pig/auth/config/PigAuthorizationConfig.java b/pig-auth-service/src/main/java/com/github/pig/auth/config/PigAuthorizationConfig.java index cdad987f..9cba74f0 100644 --- a/pig-auth-service/src/main/java/com/github/pig/auth/config/PigAuthorizationConfig.java +++ b/pig-auth-service/src/main/java/com/github/pig/auth/config/PigAuthorizationConfig.java @@ -7,6 +7,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.security.authentication.AuthenticationManager; 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.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; @@ -53,6 +55,11 @@ public class PigAuthorizationConfig extends AuthorizationServerConfigurerAdapter .userDetailsService(userDetailsService); } + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + @Bean public JwtAccessTokenConverter jwtAccessTokenConverter() { JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();