Spring Security是为基于Spring的应用程序提供声明式安全保护的安全性框架,它提供了完整的安全性解决方案,能够在web请求级别和方法调用级别处理身份证验证和授权。因为基于Spring框架,所以Spring Security充分利用了依赖注入和面向切面的技术。
Spring Security主要是从两个方面解决安全性问题:
web请求级别:使用Servlet规范中的过滤器(Filter)保护Web请求并限制URL级别的访问。
方法调用级别:使用Spring AOP保护方法调用,确保具有适当权限的用户才能访问安全保护的方法。
配置见下:
spring-security.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!--释放静态资源-->
<security:http pattern="/login.jsp" security="none"/>
<security:http pattern="/*.html" security="none"/>
<security:http pattern="/failer.jsp" security="none"/>
<security:http pattern="/css/**" security="none"/>
<security:http pattern="/img/**" security="none"/>
<security:http pattern="/plugins/**" security="none"/>
<http pattern="/js/**" security="none"></http>
<!--
auto-config="true" 表示springsecurity框架会自动寻找认证页面
如果我们自己没有提供,则使用springsecurity默认的认证页面
如果我们自己有认证页面,需要配置才能使用
use-expressions="true" 如果是true表示使用spring的el表达式进行配置
如果是false则可以直接配置
-->
<security:http auto-config="true" use-expressions="true">
<!--对当前项目的所有资源进行拦截-->
<!--
<security:intercept-url pattern="/**" 表示要拦截项目中的所有资源
access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"
当用户有ROLE_USER角色或者ROLE_ADMIN角色的时候才能访问资源
其中springsecurity规定角色的名称必须以ROLE_开头。
-->
<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>
<!--配置自己的认证页面-->
<security:form-login login-processing-url="/login"
login-page="/login.jsp"
default-target-url="/index.jsp"
authentication-failure-url="/failer.jsp"/>
<!--配置退出认证信息-->
<security:logout logout-url="/logout"
invalidate-session="true"
logout-success-url="/login.jsp"/>
<!--关闭csrf拦截-->
<security:csrf disabled="true"/>
<security:access-denied-handler error-page="/403.jsp"/>
</security:http>
<!--密码加密器-->
<bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
<!--
<security:authentication-manager> security的认证管理[登录信息的来源]
目前<security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
是在缓存中放入了一个用户。用户名为admin密码为admin当前用户的角色为ROLE_USER
springsecurity的认证默认是必须加密的。
如果想不加密认证必须在密码前面加上{noop}
-->
<security:authentication-manager>
<security:authentication-provider user-service-ref="userServiceImpl">
<security:password-encoder ref="encoder"/>
<!--<security:user-service>
<security:user name="admin" password="{noop}admin" authorities="ROLE_ADM"/>
<security:user name="user" password="{noop}user" authorities="ROLE_USER"/>
</security:user-service>-->
</security:authentication-provider>
</security:authentication-manager>
<security:global-method-security jsr250-annotations="enabled"
secured-annotations="enabled"
pre-post-annotations="enabled"/>
</beans>
web.xml的配置文件
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
在sping-context中需要导入
<import resource="classpath:spring-security.xml"/>
详细见大神“江南一点雨”的博客,有更为详细是的使用方法。
https://www.cnblogs.com/lenve/p/11242055.html
另:在word中优雅的插入代码块:
将代码复制到这个网站:http://www.planetb.ca/syntax-highlight-word
选择合适的编码语言,然后 ShowHighlighted 就OK了!