目前我正在使用Spring Boot 1.4.0版本進行持續開發,使用Spring安全性進行身份驗證。要求是用戶首次登錄時需要重定向到密碼重置頁面,否則應重定向到主頁面。無論在成功處理程序中配置的URL如何,應用程序總是重定向home.jsp。春季開機安全始終重定向到home.jsp
下面是我的配置,我失去了什麼這裏
WebSecurityConfiguration
http.authorizeRequests()
.antMatchers("/resources/**","/rest/**","/log*")
.permitAll()
.antMatchers("/admin**").hasAuthority("admin")
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.successHandler(authHandler)
.failureHandler(authFailureHandler)
.usernameParameter("username").passwordParameter("password")
.permitAll()
.and()
.logout()
.invalidateHttpSession(true)
.logoutSuccessUrl("/login?logout")
.permitAll()
.and()
.csrf().disable();
公共類AuthSuccessHandler擴展SimpleUrlAuthenticationSuccessHandler {
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
HttpSession session = request.getSession(false);
String isFirstTimePwd = String.valueOf(session.getAttribute("IsFirstTimeLogIn"));
if (isFirstTimePwd.equalsIgnoreCase("true"))
{
redirectStrategy.sendRedirect(request,response,"/firstTime");
}
else
{
redirectStrategy.sendRedirect(request, response, "/home");
}
}
}
@RequestMapping(value = "/firstTime", method = RequestMethod.GET)
public String displayFirstTimeLoginPage(HttpServletRequest request,HttpServletResponse response) {
return "firstTime";
}
@RequestMapping(value = "/home", method = RequestMethod.GET)
public ModelAndView homePage(HttpServletRequest request,HttpServletResponse response) {
HttpSession session = request.getSession();
User user =(User) session.getAttribute("User");
return new ModelAndView("home", "loggedInUser", user);
}
而且我嘗試通過重寫onAuthenticationsuccess()來實現authenticationsuccesshandler,但仍然重定向home.jsp,而不是密碼重置頁面。
你嘗試調試找到什麼isFirstTimePwd.equalsIgnoreCase的'(價值」真「)? – chaoluo
是的,它的第一次控制器映射和重定向密碼重置頁面之後,立即重定向到home.jsp,如果我將home.jsp重命名爲index.jsp其未顯示的文件。 – aap
@ap:你必須重寫'onAuthenticationsuccess()'。顯示級別爲「DEBUG」的Spring安全日誌。它會顯示,爲什麼你沒有被重定向到正確的頁面。 – dur