1. 程式人生 > >spring boot結合spring security實現註冊後自動登入

spring boot結合spring security實現註冊後自動登入

spring boot結合spring security實現使用者註冊後自動登入
在開發過程中,有些頁面只有使用者登入之後才能訪問,比如使用者的個人中心,但是按道理使用者註冊之後也應該有許可權進入了個人中心,下面即是實現使用者註冊後自動登入的方法~~記錄一下

    @Autowired
    protected AuthenticationManager authenticationManager;
public void shili(HttpServletRequest request){
                UsernamePasswordAuthenticationToken token=new
UsernamePasswordAuthenticationToken(name,password); token.setDetails(new WebAuthenticationDetails(request)); Authentication authenticatedUser=authenticationManager.authenticate(token); SecurityContextHolder.getContext().setAuthentication(authenticatedUser); request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());}

name,password分別為使用者名稱和密碼~~