如果要对Web资源进行保护,最好的办法莫过于Filter
如果要想对方法调用进行保护,最好的办法莫过于AOP
Spring Security 作为一个单独的过滤器安装在链中,其配置类型为 FilterChainProxy。
Spring Security 可以根据访问地址进行细化:
安全过滤器链(或等同于WebSecurityConfigurerAdapter
)具有请求匹配器,用于决定是否将其应用于HTTP请求。 一旦决定采用特定的过滤器链,则不会应用其他过滤器。
这里可以跟spring security的核心类进行对应一下:
【备注】有些书也会把AuthenticationManager
等同于非父类的那些ProviderManager
,就是除了顶上那个这张图里的其他ProviderManager
- 每个人都是一个 ProviderManager,他们共享一个父类。
- 有时应用程序具有受保护资源的逻辑组(例如所有与路径模式/ api / **相匹配的Web资源),并且每个组可以具有其自己的专用
AuthenticationManager
; - 父类是一种“全局”资源,充当所有提供者的失败回调。如果所有的提供者返回null,则将再交给父类去认证。 如果父类不可用,则会导致
AuthenticationException
。
参考:http://www.blogjava.net/youxia/archive/2008/12/07/244883.html
课程:https://www.iteye.com/blogs/subjects/spring_security
w3school:https://www.w3cschool.cn/springsecurity/cdz11ihv.html
慕课网的视频教程(springboot下的spring security配置):https://www.jianshu.com/p/24c6a65c3913
比较接近SSM下的spring security配置:https://wenku.baidu.com/view/e05cef2d00f69e3143323968011ca300a6c3f6e6.html
spring-security的核心能力:
- 认证(你是谁)
- 授权(你能干什么)
- 攻击防护(防止伪造身份)
原理:在web.xml中配置的一个代理,web.xml中的代理依次调用下面的Bean,就实现了对Web资源的保护。同时这些Filter作为Bean被Spring管理。
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置文件spring-security要在tomcat启动的时候被加载
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext.xml,/WEB-INF/config/spring-security.xml</param-value> <!--把spring security的xml加进去-->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
spring-security.xml
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/security"
xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.1.xsd">
<!--
1、设置放行资源,如登录注册页面,静态资源css、js等等
security="none" 设置此资源不被拦截.
-->
<http pattern="/login.jsp" security="none"></http>
<http pattern="/loginerror.html" security="none"></http>
<http pattern="/css/**" security="none"></http>
<http pattern="/img/**" security="none"></http>
<http pattern="/js/**" security="none"></http>
<http pattern="/plugins/**" security="none"></http>
<http>
<!-- 2、拦截所有(除放行资源外) -->
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<!--
3、登录表单设置
1)login-page:指定登录页面;
2)login-processing-url:指定登录请求路径;
3)default-target-url:指定了成功进行身份验证和授权后默认呈现给用户的页面;
4)always-use-default-target:指定了是否在身份验证通过后总是跳转到;
default-target-url 属性指定的 URL。
5)authentication-failure-url:指定了身份验证失败时跳转到的页面;
-->
<form-login login-page="/login.jsp"
login-processing-url="/login"
always-use-default-target="true"
default-target-url="/index.jsp"
authentication-failure-url="/loginerror.html"
/>
<!-- 4、注销设置
1)logout-url:指定注销的url;
2)logout-success-url:注销成功后登录返回的页面。
-->
<logout logout-url="/logout" logout-success-url="/login.html"/>
<!--
5、跨站请求设置(我们这里关闭)
1)csrf disabled="true" 关闭 csrf ,如果不加会出现错误
2)CSRF(Cross-site request forgery):跨站请求伪造,
也被称为“One Click Attack”或者 SessionRiding,
通常缩写为 CSRF 或者 XSRF,是一种对网站的恶意利用。
-->
<csrf disabled="true" />
<!-- 6、iframe 框架结构展示 -->
<headers>
<frame-options policy="SAMEORIGIN" />
</headers>
</http>
<!--
认证管理器
1)我们这里设置一个默认用户
-->
<authentication-manager>
<authentication-provider>
<user-service>
<user authorities="ROLE_USER" name="admin" password="123456" />
</user-service>
</authentication-provider>
</authentication-manager>
</bean:beans>
这个文件说明了两件事情:
- 对所有
http
的请求(pattern="/**"
)进行拦截; - 加入一个
authentication-manager
, 里面提供一种认证方式,就是对账号密码进行验证;
注意,需要自己增加一个login.jsp的登陆页和登陆成功后默认会跳转的index.jsp页面
【参考】https://blog.csdn.net/houysx/article/details/80380831
这个XML如何解释的代码:https://www.cnblogs.com/xhj123/p/6193864.html
过滤器链的顺序是非常重要的,接下来看看过滤链的顺序。
常用的filter:
- 制定必须为https连接;
- 从Session中提取用户的认证信息;
- 退出登录;
- 登录;
- 记住用户;
- 所有的应用必须配置这个Filter。