springSecurity实战

18.6.12
@AuthenticationPrincipal注解
相当于Authentication.getPrincipal()
可以用于
public Object login(@AuthenticationPrincipal User loginedUser, @RequestParam(name = "logout", required = false) String logout) {

18.6.8
1、实现的主要流程

1.创建一个Filter 继承 AbstractSecurityInterceptor

  • FilterSecurityInterceptor extends AbstractSecurityInterceptor
    • 是过滤链中最后一个Filter
    • 使用SecurityContext 和 Authentication 对象,来进行辨别用户的 GrantedAuthority。
  • 为了实现自定义过滤,需要Authentication和SecurityContext,所以继承AbstractSecurityInterceptor,并且放在FilterSecurityInterceptor之前

2.该过滤器需要注入FilterInvocationSecurityMetadataSource,AccessDecisionManager 和 UserDetailsService

  • FilterInvocationSecurityMetadataSource 在spring初始化时获取用户权限资源关系
  • AccessDecisionManager 用于判断当前用户是否有权限
  • UserDetailsService 用于根据当前用户获取其权限

3.扩展 FilterInvocationSecurityMetadataSource,AccessDecisionManager 和 UserDetailsService,使其使用数据库的数据

参考https://www.cnblogs.com/final-elysion/p/6278180.html

2、配置自定义 Filter 在 Spring Security 过滤器链中的位置
参考:https://www.jianshu.com/p/deb512b41f99
https://blog.csdn.net/win7system/article/details/51659182
https://www.cnblogs.com/fenglan/p/5913248.html

3、默认登录验证是通过用户名和密码,如果想要通过其他方式验证,需要扩展AuthenticationProvider接口,该接口有不少抽象类或实现类,直接继承就好。
参考:https://www.jianshu.com/p/955e30866121
http://salever.iteye.com/blog/1686425
https://blog.csdn.net/chaozhi_guo/article/details/46365735

4、匿名认证的源码解析
参考:https://blog.csdn.net/caomiao2006/article/details/51812440
————————
18.6.7
1、关于Http认证方式的详解:
参考:https://blog.csdn.net/u013177446/article/details/54135520
https://blog.csdn.net/a464057216/article/details/52705855
2、
http
.authorizeRequests() 1
.antMatchers( "/resources/", "/signup" , "/about").permitAll() 2
.antMatchers( "/admin/
").hasRole("ADMIN" ) 3
.antMatchers( "/db/**").access("hasRole('ADMIN') and hasRole('DBA')") 4
.anyRequest().authenticated()
前面指定访问的路径对应的权限,按顺序校验,后面任何请求都需要进行权限认证。
参考:https://blog.csdn.net/carrie__yang/article/details/77504778?locationNum=3&fps=1
————————
18.5.23
需要写自己的springsecurity配置类,集成WebSecurityConfigurerAdapter
WebSecurityConfig extends WebSecurityConfigurerAdapter
需要重写两个方法
//用于配置登录相关,匹配的url、匹配的权限、登录页面和跳转、端口号、登出相关、session相关、rememberMe等等
protected void configure(HttpSecurity http)
//用于配置获取用户信息、配置获取用户权限等等
protected void configure(AuthenticationManagerBuilder auth)
————————
ExpressionInterceptUrlRegistry authorizeRequests()//返回跟request的配置
该配置下有如下三个内部类:
ExpressionInterceptUrlRegistry//与表达式拦截相关的功能在此
AbstractInterceptUrlRegistry//与普通路径拦截相关的功能在此
AuthorizedUrl//与权限相关的功能在此

有一个常用的方法and(),返回调用它的方法的返回类型。
也就是说and()返回的类型与调用它的方法的返回类型相同。
它的存在意义在于隔离,对于同一个配置进行多个不同的功能的配置,使用链式表达很难区分,就是用and()隔离一下。仅此而已。

————————
其他参考:https://blog.csdn.net/icarusliu/article/details/78722384
http://elim.iteye.com/blog/2155786#_Toc403683388
https://www.cnblogs.com/quyixuanblog/p/5213503.html
https://www.cnblogs.com/fenglan/p/5913387.html
https://blog.csdn.net/xichenguan/article/details/77892913
https://www.jianshu.com/p/955e30866121
https://blog.csdn.net/d7011800/article/details/8692667
https://blog.csdn.net/u013516966/article/details/46688765
https://blog.csdn.net/rongku/article/details/51235694

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,073评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,135评论 25 708
  • 什么叫“理财思维的身体力行者”?所谓身体力行,就是自己亲自去体验,努力去实践。要求或者建议别人怎么做的时候,自己先...
    888210ed86d2阅读 204评论 1 1
  • 过年那段时间总能看到很多小伙伴在朋友圈发聚会照片,整个微信都弥漫这一种情意浓浓的温暖感,仿佛七色彩虹圈圈一转整个界...
    冷先生慢热中阅读 472评论 0 0
  • 复习一下Liunx常用命令。 文件部分 ls命令 [1] 对应文件权限: [-]表示文件。 [d]表示目录。 [l...
    ffusheng阅读 422评论 0 0

友情链接更多精彩内容