spring-security的配置

2 篇文章 0 订阅

spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 下面这个事前缀 若是用默认的beans即可不用前缀 因为后面要配置的security的标签很多 为了方便就直接给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.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>
	
		<!-- 页面的拦截规则 use-expression:是否启动SPEL表达式默认是true-->
	<http use-expressions="false">
	<!-- 这里的/*只包括这个跟目录本机的资源 /**才是包括子目录的资源 与web.xml不一样 -->
	<!-- access当前用户必须有ROLE_USER的角色 才可以访问根目录及所属子目录的资源 -->
		<intercept-url pattern="/**" access="ROLE_USER"/>
		<!-- 开启表单登录功能 form-login能很智能的自动生成一个表单登录界面-->
		<form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>
		<csrf disabled="true"/>
	</http>	
<!-- 若是启动SPEL表达式要这样写-->
<!--  <http >
		<intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
	</http>	 -->
	
	<!-- 认证管理器 -->
	<authentication-manager>
		<authentication-provider>
			<user-service>
				<user name="admin" password="123456" authorities="ROLE_USER"/>
			</user-service>
		</authentication-provider>
	</authentication-manager>
	
			
</beans:beans>

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>  
		<!--
			 过滤器代理类 由spring web 包提供  作用是将这个请求经过这个过滤器之后,他会自动寻找一个
			名为springSecurityFilterChain的类,注意这个名称不要改,他就是这个安全过滤框架的入口
		-->
		<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>

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值