【问题标题】:I am using spring security 3.2.7 with tomcat 6 and I weed to make logout button我正在将 spring security 3.2.7 与 tomcat 6 一起使用,并且我正在制作注销按钮
【发布时间】:2016-11-30 11:35:00
【问题描述】:

我正在使用带有spring security 3.2.7的angularjs并在tomcat 6上部署使用此代码进行登录操作

@RequestMapping(value = "/login", method = RequestMethod.POST)
public @ResponseBody Map<String, ? extends Object> login(
                        HttpServletRequest request,
                        @RequestParam String userName,
                        @RequestParam String password) {
    Map<String, Object> response = new HashMap<String, Object>();
    logger.info("RegistreController Login...");
    logger.info("Start Login for the user :"+userName);
    try{
    String encodedPassword = hashPassword(password);
    System.out.println("encodedPassword = "+encodedPassword);

    final UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(userName, encodedPassword);

    // this function returns UsernamePasswordAuthenticationToken(user, password, authorities)
    final Authentication authentication = authenticationProvider.authenticate(authRequest);

    SecurityContextHolder.getContext().setAuthentication(authentication);
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    CustomUser user = null;
    if (principal instanceof CustomUser) {
        user = ((CustomUser)principal);
    }
}catch(Exception e) {
     if(e instanceof CredentialsExpiredException){
         response.put("expired", true);
     }
     response.put("success", false);
     response.put("msg", e.getMessage());
     logger.fatal(new MasterProtectionLogger().reportError("UserAdminController.login()", e, logger));
}
  return response;
}

我需要通过代码编写注销(不调用 j_spring_security_logout) 我该怎么做?

【问题讨论】:

    标签: spring-security tomcat6


    【解决方案1】:

    关键是:

    SecurityContextHolder.clearContext();
    

    您可能还想使 HTTP 会话无效。

    【讨论】:

      猜你喜欢
      • 2016-10-13
      • 2011-04-16
      • 1970-01-01
      • 2011-08-05
      • 2019-09-12
      • 1970-01-01
      • 2019-03-04
      • 2011-10-05
      • 1970-01-01
      相关资源
      最近更新 更多