重新加载后丢失身份验证

时间:2017-06-16 09:01:14

标签: spring authentication spring-boot spring-security

在浏览器中重新加载页面后,我丢失了手动设置的身份验证。 当我第一次从下面的代码片段加载控制器时,变量test_if_session_is_theretest_if_auth_is_there当然是空的。

但是在重新加载控制器(浏览器中的F5)之后,变量test_if_session_is_there取值#34;会话在那里"喜欢它,但test_if_auth_is_there保持为空。

如果我在设置之后立即执行SecurityContextHolder.getContext().getAuthentication();(在重新加载之前),我会获得所需的身份验证,但在重新加载后,它就会消失。

有人可以告诉我,为什么会这样?

@Controller
public class MyAuthQuestionController {

    @RequestMapping("/test")
    public void entry(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        String test_if_session_is_there = (String) session.getAttribute("Test");
        Authentication test_if_auth_is_there = SecurityContextHolder.getContext().getAuthentication();

        session.setAttribute("Test", "Session is there");
        UserDetailsAdapter userDetailsAdapter = new UserDetailsAdapter(new UserAccount());
        Authentication newAuth = new 
        UsernamePasswordAuthenticationToken(userDetailsAdapter,"", userDetailsAdapter.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(newAuth);
    }
}

0 个答案:

没有答案