在SecurityConfig类的configure方法中添加如下
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.formLogin()
.loginPage("/login")
.successHandler(new LoginSuccessHandler())
.permitAll()
}
实现登录成功后的处理器LoginSuccessHandler
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
if(roles.contains("ROLE_admin")){
httpServletResponse.sendRedirect("/admin/index");
}else {
httpServletResponse.sendRedirect("/index");
}
}
}
在AdminController类中添加主页跳转控制的方法
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
@RequestMapping(value = {"/","/index"})
public String index(){
return "admin/index";
}
}