zwzw1219 2017-10-14 18:05:40 +08:00
parent af4ab8badc
commit 6fa512f23c
3 changed files with 25 additions and 16 deletions

View File

@ -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));
}
};

View File

@ -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("用户名不存在");

View File

@ -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());
}
}