Spring security主要是从两个方面解决安全性问题:
- web请求级别:使用servlet过滤器保护web请求并限制URL级别的访问
- 方法调用级别:使用Spring AOP保护方法调用,确保具有适当权限的用户采用访问安全保护的方法.
一、引入jar包
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
二、web.xml配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<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>
DelegatingFilterProxy
是一个Spring Framework类,它委托给一个过滤器实现,该过滤器实现在应用程序上下文中定义为一个Spring bean。 在这种情况下,bean名为springSecurityFilterChain
,它是由命名空间创建的内部基础结构bean,用于处理Web安全。 注意,你不应该自己使用这个bean名称。 将它添加到web.xml后,就可以开始编辑应用程序上下文文件。 Web安全服务使用<http>
元素配置。三、spring-security.xml配置文件分析
spring-security-config.
jar。 然后你需要做的是将模式声明添加到应用程序上下文文件中:<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
...
</beans>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
...
</beans:beans>
<security:http auto-config="true" use-expressions="false"> <!--use-expressions 默认为true--> <security:intercept-url pattern="/index" access="ROLE_USER"/> </security:http>
设置auto-config="true"会自动生成一个登陆界面,
Spring security 3.0以后加入了对SpEL的支持,可以将<http>元素的use-expressions设置为"true"便可使用SpEL
Spring Security 支持的所有SpEL表达式如下:
安全表达式 计算结果 authentication 用户认证对象 denyAll 结果始终为false hasAnyRole(list of roles) 如果用户被授权指定的任意权限,结果为true hasRole(role) 如果用户被授予了指定的权限,结果 为true hasIpAddress(IP Adress) 用户地址 isAnonymous() 是否为匿名用户 isAuthenticated() 不是匿名用户 isFullyAuthenticated 不是匿名也不是remember-me认证 isRemberMe() remember-me认证 permitAll 始终true principal 用户主要信息对象
use-expressions 默认为true,当use-expressions ='true'时; <security:intercept-url pattern="/index" access="hasRole('ROLE_USER')"/>接口access必须使用spel表达式,use-expressions ='false'时,接口不能使用spel表达式。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security" 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.0.xsd"> <security:http use-expressions="true"> <security:form-login login-page="/open/login.html" login-processing-url="/j_spring_security_check" username-parameter="username" password-parameter="password" authentication-success-handler-ref="loginSuccessHandlerRef" authentication-failure-url="/open/login.html" /> <security:logout logout-url="/open/loyout" logout-success-url="/open/login.html" invalidate-session="true"/> <security:access-denied-handler error-page="/open/accessDeny.html"/> <security:intercept-url pattern="/resource/**" access="permitAll"/> <security:intercept-url pattern="/" access="permitAll" /> <security:intercept-url pattern="/open/**" access="permitAll"/> <security:intercept-url pattern="/secure/**" access="fullyAuthenticated"/> <security:intercept-url pattern="/**" access="denyAll"/> <security:csrf disabled="true"/> </security:http> <bean id="loginSuccessHandlerRef" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"> <property name="defaultTargetUrl" value="/secure/index.html"/> </bean> <security:authentication-manager> <security:authentication-provider user-service-ref="testUserDetailsService"> <security:password-encoder ref="passwordEncoder"/> </security:authentication-provider> </security:authentication-manager> <bean id="passwordEncoder" class="com.test.common.security.TestPasswordEncoder"></bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:org/springframework/security/messages" /> </bean> </beans>
现在,配置基本完成,启动项目后,在登录页面输入用户名和密码,不管正确与否都会有405错误Request method ‘POST’ not supported。为什么会出现这个错误呢?
登录请求405错误
在登录请求中出现405错误,其实是我们在配置过程中,spring mvc和spring security出现了一点冲突导致的。
首先,请注意下面这两个配置段
<http pattern="/login" security="none"></http>
<form-login login-page="/login" login-processing-url="/login" ......
第一个配置告诉spring security,类似于/login的url请求不做过滤处理,而第二个配置信息又告诉spring security url为/login的post请求登录请求处理。正是这种冲突导致了405错误的发生。
既然知道了错误原因,那么只要避免冲突就能解决这个问题:使登录页的请求和登录处理的请求不一致,然后只配置登录页的请求不做拦截处理.
我采用的方法是使用默认的登录URL /login,修改登录页面跳转url为/loginPage。请自行修改代码和配置信息
这样是不是就大工告成了呢?很遗憾,当你启动程序时,输出用户名和密码,不管正确与否,都会有404 Not Found等着你,这又是为什么呢?
登录请求404错误
首先,建议你看一下spring security自动生成的登录页源码,你会发现有如下代码
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
对于什么是csrf,请自行参考网上的资料。
spring security默认情况下csrf protection是开启的,由于我们的登录页没有配置csrf的相关信息,因此spring security内置的过滤器将此链接置为无效链接
解决办法就是配置csrf protection为不可用状态,在配置文件中增加
<csrf disabled="true"/>
4)异常信息本地化
<!--spring security 异常信息本地化--> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:org/springframework/security/messages"/> </bean>