1 2 3 4 5 6 7 8 | public Authentication attemptAuthentication(HttpServletRequest request,HttpServletResponse response) throws AuthenticationException { ...... UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password); setDetails(request, authRequest); return this.getAuthenticationManager().authenticate(authRequest); } |
1 2 3 4 5 6 | protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult)throws IOException, ServletException { ...... SecurityContextHolder.getContext().setAuthentication(authResult); ...... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public Authentication authenticate(Authentication authentication) throws AuthenticationException { ...... // 获取 UserDetails UserDetails user = this.userCache.getUserFromCache(username); if (user == null) { cacheWasUsed = false; try { user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication); } ...... } ...... try { ...... //校验密码 additionalAuthenticationChecks( user, (UsernamePasswordAuthenticationToken) authentication ); } ...... // 从新注入 Authentication return createSuccessAuthentication( principalToReturn, authentication, user ); } |
1 2 3 4 5 6 7 8 9 10 | protected final UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { UserDetails loadedUser; try { loadedUser = this.getUserDetailsService().loadUserByUsername(username); } ...... return loadedUser; } |
1 2 3 | public interface UserDetailsService { UserDetails loadUserByUsername(String username) throws UsernameNotFoundException; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class CustomUserDetailsService implements UserDetailsService { @Autowired private UserDao userDao; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userDao.findByName(username); if(Objects.isNull(user)) { throw new UsernameNotFoundException(); } UserDetails details = new CustomUserDetails(user); return details; } } |
1 2 3 4 5 6 7 8 9 10 11 | protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) { UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken( principal, authentication.getCredentials(), authoritiesMapper.mapAuthorities(user.getAuthorities()) ); result.setDetails(authentication.getDetails()); return result; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |