spring securiy使用总结

我们常见的几个功能:

  1. 注册后直接登录,并且remember-me
    这种在网上找到很多注册后登录的,但是remember-me没有。
    其实解决方案还是看源码比较方便。
    a. 装载authenticationManager

    @Autowired

     private AuthenticationManager authenticationManager;


    b. 装载rememberMeServices,注意一定要AbstractRememberMeServices,因为它能修改alwaysremember属性
    @Autowired

     private AbstractRememberMeServices rememberMeServices;

    c.  自动登录并remember 

             

UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(

     user.getMobile(), password);

   Authentication authentication = authenticationManager.authenticate(authRequest);

   rememberMeServices.setAlwaysRemember(true);

 rememberMeServices.loginSuccess(request, response, authentication);    

                 2. @PreAuthorize不work的情况

             不是网上搜的加上 pre-post-annotations="enabled"就完事,一定要加在你的应用程序配置里,而不是spring-security.xml里

             如果不太理解是哪个文件,那么<annotation-driven>在哪个文件,你就加在哪个文件

原文地址:https://www.cnblogs.com/xxoome/p/5861435.html