Spring Security 安全框架

一 简介:Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoCDI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作.

 Spring Security入门小Demo

1.创建工程spring-security-demo 引入pom依赖

<--spring相关依赖  略-->
<--security相关依赖-->

<dependency>

  <groupId>org.springframework.security</groupId>

  <artifactId>spring-security-web</artifactId>

  <version>4.1.0.RELEASE</version>

</dependency>

 <dependency>

  <groupId>org.springframework.security</groupId>

  <artifactId>spring-security-config</artifactId>

  <version>4.1.0.RELEASE</version>

</dependency>

2.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

    <context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath: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>

</web-app>

3.创建index.xml

4.配置spring-security.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<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.xsd

http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

<!-- 页面拦截规则 -->

<http use-expressions="false">

<intercept-url pattern="/**" access="ROLE_USER" />

<form-login/>

</http>

<!-- 认证管理器 -->

<authentication-manager>

<authentication-provider>

<user-service>

<user name="admin" password="123456" authorities="ROLE_USER"/>

</user-service>

</authentication-provider>

</authentication-manager>

</beans:beans>

use-expressions 为是否使用使用 Spring 表达式语言( SpEL ,默认为true ,如果开启,则

拦截的配置应该写成以下形式

<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />

这样就可以实现拦截了  不过默认有登陆界面   也可以自己定义登陆界面

1.首先自定义一个登陆界面(一个成功界面,一个失败界面)

2.修改spring-security.xml

<!-- 以下页面不被拦截 -->

<http pattern="/login.html" security="none"></http>

<http pattern="/login_error.html" security="none"></http>

<!-- 页面拦截规则 -->

<http use-expressions="false">

<intercept-url pattern="/*" access="ROLE_USER" />

<form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>

<csrf disabled="true"/>

</http>

security="none"  设置此资源不被拦截.

login-page:指定登录页面。

authentication-failure-url:指定了身份验证失败时跳转到的页面。

default-target-url:指定了成功进行身份验证和授权后默认呈现给用户的页面。

csrf disabled="true"  关闭csrf ,如果不加会出现错误

ok.

猜你喜欢

转载自www.cnblogs.com/superbc/p/10117260.html