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;
    }

  • 相关阅读:
    571B. Minimization(Codeforces Round #317)
    java的死锁学习
    算法——大整数乘法
    从头认识java-15.7 Map(3)-介绍HashMap的工作原理-get方法
    软硬件之共生之道——一千零一夜的启发
    Java系列之JNDI
    the solution of CountNonDivisible by Codility
    qml
    日历日历日历
    项目总结——传说中的反射居然是这个样子
  • 原文地址:https://www.cnblogs.com/yizw/p/10483918.html
Copyright © 2011-2022 走看看