Spring-Security安全框架

概念

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

功能实现依赖:

Spring Security对Web安全性的支持大量地依赖于Servlet过滤器

使用方法

1.pom中导入相关依

			 <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>
			
           <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
            </dependency>

            <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-cas</artifactId>
            <version>4.1.0.RELEASE</version>
            </dependency>

配置web.xml文件

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

配置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.xsd">

<!-- 配置放行资源 -->
<!-- *代表的是根目录,**代表的是根目录下及下面目录的资源,因为目录下可能还有目录-->
    <http  pattern="/login.html" security="none"></http>
    <http  pattern="/login_error.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>
    
<!-- 配置拦截资源  -->
<!--  拦截路径 '/*'拦截根目录下所有资源 access="hasRole('ROLE_USER')" 代表拥有的角色 代表用户权限 ROLE_前缀  -->
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
    <!-- login-page="/login.html" 默认登录地址 
    default-target-url="/index.html" 登录成功后跳转的页面
    default-target-url="/index.html" always-use-default-target="true" 必须结合使用
    authentication-failure-url="/login_error.html" 登录失败跳转该页面
    -->
    <form-login login-page="/login.html" 
    default-target-url="/admin/index.html" 
    always-use-default-target="true" 
    authentication-failure-url="/login_error.html"  
   
    />
    <logout/>
   <!-- 关闭跨域攻击 -->
    <csrf disabled="true"/>
	<!--注意事项:spring-secuitry默认拦截iframe框架包含的html
	如果允许访问必须配置
	 -->
     <headers>
	<frame-options policy="SAMEORIGIN"/>
     </headers>
	<!--注销用户-->
    <logout/>
</http>
<!-- 配置认证管理器
   1.提供者
   2.提供者配置用户名 和密码
   authorities=""拥有的权限
 -->
<authentication-manager>
    <authentication-provider>
        <user-service>
          <user name="admin"  password="123" authorities="ROLE_USER"/>
        </user-service>
    </authentication-provider>
</authentication-manager>
	
</bean:beans>

html中的使用

登录操作

<form class="sui-form" action="/login" method="post" id="formlogin">
<a class="sui-btn btn-block btn-xlarge btn-danger" onclick="document:formlogin.submit()"target="_blank">&nbsp;&nbsp;</a>

注意这个action是写成“login”,有security框架帮我们处理

注销操作

<a href="/logout"  class="btn btn-default btn-flat">注销</a>

加此配置后,会自动的产生退出登录的地址/logout,如果你不想用这个地址 ,你也可以定义
生成的退出地址以及跳转的页面,配置如下

<logout logout-url="" logout-success-url=""/>

logout-url:退出的地址,会自动生成

logout-success-url:退出后跳转的地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值