zoukankan      html  css  js  c++  java
  • 代码登录spring security并且获取jsessionId

    @Autowired
    @Qualifier("org.springframework.security.authenticationManager")
    protected AuthenticationManager authenticationManager;

    @RequestMapping(value = "/test")
    public ModelAndView test(HttpServletRequest request,HttpServletResponse response){

    //跳转首页
    ModelAndView view = new ModelAndView("pages/index");

    //使用用户名、密码生成可用AuthenticationToken(用户名:test,密码:123456)
    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken("test", "123456");

    //设置authenticationToken的details,主要获取请求信息
    authenticationToken.setDetails(new WebAuthenticationDetails(request));

    //使用authenticationManager接口中的anthenticate进行springsecurity认证
    Authentication authenticatedUser = authenticationManager.authenticate(authenticationToken);

    //将认证信息放入安全上下文中(此处为个人理解)
    SecurityContextHolder.getContext().setAuthentication(authenticatedUser);

    //如果没有session,生成一个session并设置当前的securityContext
    request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());

    //此sessionId为响应给浏览器的jsessionId(可在浏览器中查看cookie中的jsessionId与此值是否相等)
    String sessionId = request.getSession().getId();

    System.out.println(jsessionId);
    return view;
    }

  • 相关阅读:
    前端小tite(随笔)
    算法两数之和 python版
    常用标签
    pip install 遇到的问题
    不常用的模块
    约束和约束关系
    Django初识
    前端—Bootstrap
    前端—jQuery
    前端—BOM和DOM
  • 原文地址:https://www.cnblogs.com/yizw/p/10483918.html
Copyright © 2011-2022 走看看