From 6fa512f23cd7e44b4744a1feeac03e5b45fc8f7b Mon Sep 17 00:00:00 2001 From: zwzw1219 Date: Sat, 14 Oct 2017 18:05:40 +0800 Subject: [PATCH] 1 --- .../server/config/SecurityHandlerConfig.java | 14 ++--------- .../service/impl/UserDetailsServiceImpl.java | 4 ---- .../server/SecurityApplicationTest.java | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/boot/security/server/SecurityApplicationTest.java diff --git a/src/main/java/com/boot/security/server/config/SecurityHandlerConfig.java b/src/main/java/com/boot/security/server/config/SecurityHandlerConfig.java index dc8f927..a211177 100644 --- a/src/main/java/com/boot/security/server/config/SecurityHandlerConfig.java +++ b/src/main/java/com/boot/security/server/config/SecurityHandlerConfig.java @@ -10,9 +10,6 @@ import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; -import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; -import org.springframework.security.authentication.BadCredentialsException; -import org.springframework.security.authentication.LockedException; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.userdetails.User; @@ -64,15 +61,8 @@ public class SecurityHandlerConfig { @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { - ResponseInfo info = ResponseInfo.builder().code(HttpStatus.UNAUTHORIZED.value() + "").build(); - if (exception instanceof AuthenticationCredentialsNotFoundException) { - info.setMessage("用户名存在"); - } else if (exception instanceof LockedException) { - info.setMessage("用户被锁定"); - } else if (exception instanceof BadCredentialsException) { - info.setMessage("密码错误"); - } - + ResponseInfo info = ResponseInfo.builder().code(HttpStatus.UNAUTHORIZED.value() + "") + .message(exception.getMessage()).build(); writeResponse(response, HttpStatus.UNAUTHORIZED.value(), JSONObject.toJSONString(info)); } }; diff --git a/src/main/java/com/boot/security/server/service/impl/UserDetailsServiceImpl.java b/src/main/java/com/boot/security/server/service/impl/UserDetailsServiceImpl.java index b729981..3ad49db 100644 --- a/src/main/java/com/boot/security/server/service/impl/UserDetailsServiceImpl.java +++ b/src/main/java/com/boot/security/server/service/impl/UserDetailsServiceImpl.java @@ -19,9 +19,6 @@ import com.boot.security.server.model.SysUser; import com.boot.security.server.model.SysUser.Status; import com.boot.security.server.service.UserService; -import lombok.extern.slf4j.Slf4j; - -@Slf4j @Service public class UserDetailsServiceImpl implements UserDetailsService { @@ -30,7 +27,6 @@ public class UserDetailsServiceImpl implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - log.info(username); SysUser sysUser = userService.getUser(username); if (sysUser == null) { throw new AuthenticationCredentialsNotFoundException("用户名不存在"); diff --git a/src/test/java/com/boot/security/server/SecurityApplicationTest.java b/src/test/java/com/boot/security/server/SecurityApplicationTest.java new file mode 100644 index 0000000..0fc6657 --- /dev/null +++ b/src/test/java/com/boot/security/server/SecurityApplicationTest.java @@ -0,0 +1,23 @@ +package com.boot.security.server; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.test.context.junit4.SpringRunner; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class SecurityApplicationTest { + + @Autowired + private BCryptPasswordEncoder passwordEncoder; + + @Test + public void password() { + String string = passwordEncoder.encode("admin"); + System.out.println(string); + System.out.println(string.length()); + } +}