spring security 3 -- 自定义过滤链

23 篇文章 0 订阅
6 篇文章 0 订阅

先来配置下web.xml,HttpSessionEventPublisher是使用session管理时需要用到的

    <!-- spring security -->
	<filter>
		<filter-name>securityFilterChainProxy</filter-name>
		<filter-class>
			org.springframework.web.filter.DelegatingFilterProxy
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>securityFilterChainProxy</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

接着我们要配置一条过滤链(值得注意的是这个bean的id要跟web.xml里配置的filter-name要一致才可以)

    <!-- 自定义SPRING SECURITY过滤链 -->
	<bean id="securityFilterChainProxy"
		class="org.springframework.security.web.FilterChainProxy">
		<constructor-arg>
			<list>
				<security:filter-chain pattern="/services/**"
					filters="none" />
				<security:filter-chain pattern="/test*" filters="none" />
				<security:filter-chain pattern="/**"
					filters="concurrentSessionFilter,securityContextPersistenceFilter,logoutFilter,usernamePasswordAuthenticationFilter,rememberMeAuthenticationFilter,sessionManagementFilter,anonymousAuthFilter,exceptionTranslationFilter,filterSecurityInterceptor" />
			</list>
		</constructor-arg>
	</bean>

下面我们逐个filter来看

首先是filterSecurityInterceptor,这是资源访问第一个要过的filter,至于这里面的属性注入请看我之前的spring security3 自定义权限管理的那篇文章

<!-- 自定义UserDetailsService认证  -->
	<bean id="userDetailsService"
		class="com.shadow.security.service.UserDetailsServiceImpl" />
 
	<!-- 自定义资源权限关系认证 -->
	<bean id="accessDecisionManager"
		class="com.shadow.security.service.AccessDecisionManagerImpl" />
 
	<!-- 自定义资源权限关系集合 -->
	<bean id="securityMetadataSource"
		class="com.shadow.security.service.SecurityMetadataSourceExtendImpl">
		<property name="matcher" value="ant" />
	</bean>
 
	<!-- 自定义认证管理,资源,权限  -->
	<bean id="filterSecurityInterceptor"
		class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
		<property name="authenticationManager"
			ref="authenticationManager" />
		<property name="accessDecisionManager"
			ref="accessDecisionManager" />
		<property name="securityMetadataSource"
			ref="securityMetadataSource" />
	</bean>
        
        <!-- 页面标签权限功能依赖 -->
        <bean id="webInvocationFilter"
               class="org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator">
               <constructor-arg ref="filterSecurityInterceptor" />
        </bean>
 

然后是异常捕获的filter,里面有两个属性需要注入,authenticationEntryPoint是配置默认跳转的,accessDeniedHandler是配置当检测无权限访问跳转

<!-- 异常处理过滤器 -->
	<bean id="exceptionTranslationFilter"
		class="org.springframework.security.web.access.ExceptionTranslationFilter">
		<property name="authenticationEntryPoint"
			ref="authenticationEntryPoint" />
		<property name="accessDeniedHandler">
			<!-- 拒绝未授权访问跳转 -->
			<bean
				class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
				<property name="errorPage" value="/error/audit.jsp" />
			</bean>
		</property>
	</bean>

然后是sessionManagementFilter,这个过滤器配置是否在登录后重新生成一个session防止伪造攻击

	<!-- SESSION固化保护,以及并发控制 -->
	<bean id="sessionManagementFilter"
		class="org.springframework.security.web.session.SessionManagementFilter">
		<constructor-arg name="securityContextRepository"
			ref="securityContextRepository" />
		<property name="sessionAuthenticationStrategy"
			ref="concurrentSessionControlStrategy" />
	</bean>
 
	<!-- SESSION并发配置 -->
	<bean id="concurrentSessionControlStrategy"
		class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
		<constructor-arg name="sessionRegistry" ref="sessionRegistry" />
		<property name="maximumSessions" value="1" />
		<property name="exceptionIfMaximumExceeded" value="false" />
	</bean>
 
	<bean id="sessionRegistry"
		class="org.springframework.security.core.session.SessionRegistryImpl" />
 
	<!-- SESSION并发处理 -->
	<bean id="concurrentSessionFilter"
		class="org.springframework.security.web.session.ConcurrentSessionFilter">
		<property name="sessionRegistry" ref="sessionRegistry" />
		<property name="expiredUrl" value="/error/timeout.jsp" />
		<property name="logoutHandlers">
			<list>
				<ref bean="logoutHandler" />
			</list>
		</property>
	</bean>

然后是rememberMeAuthenticationFilter,这个过滤器主要是配置记住密码功能

    <!-- 记住密码功能(COOKIE方式) -->
	<bean id="rememberMeAuthenticationFilter"
		class="org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter">
		<property name="rememberMeServices" ref="rememberMeServices" />
		<property name="authenticationManager"
			ref="authenticationManager" />
	</bean>
	<bean id="rememberMeServices"
		class="org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
		<property name="userDetailsService" ref="userDetailsService" />
		<property name="parameter" value="rememberMe" />
		<!-- 默认时间604800秒(一个星期) -->
		<property name="tokenValiditySeconds" value="604800" />
		<property name="key" value="springRocks" />
	</bean>
	<bean id="rememberMeAuthenticationProvider"
		class="org.springframework.security.authentication.RememberMeAuthenticationProvider">
		<property name="key" value="springRocks" />
	</bean>

然后是usernamePasswordAuthenticationFilter请参考我之前的spring security3自定义权限管理那篇文章

然后是logoutFilter,这个过滤器主要是做安全注销功能,注入rememberMeServices属性是为了安全退出的时候把记住密码的状态也删除了

    <!-- 注销过滤器 -->
	<bean id="logoutFilter"
		class="org.springframework.security.web.authentication.logout.LogoutFilter">
		<constructor-arg value="/logout.jsp" />
		<constructor-arg>
			<array>
				<ref bean="logoutHandler" />
				<ref bean="rememberMeServices" />
			</array>
		</constructor-arg>
		<property name="filterProcessesUrl" value="/logout" />
	</bean>
 
	<!-- 注销监听器  -->
	<bean id="logoutHandler"
		class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler">
	</bean>

后是securityContextPersistenceFilter,这个过滤器是为了持久化SecurityContext实例

    <!-- 持久化SecurityContext过滤器 -->
	<bean id="securityContextPersistenceFilter"
		class="org.springframework.security.web.context.SecurityContextPersistenceFilter">
		<property name="securityContextRepository"
			ref="securityContextRepository" />
	</bean>
 
	<!-- 生成HttpSessionSecurityContextRepository -->
	<bean id="securityContextRepository"
		class="org.springframework.security.web.context.HttpSessionSecurityContextRepository">
		<property name="allowSessionCreation" value="true" />
		<property name="disableUrlRewriting" value="false" />
	</bean>

然后是concurrentSessionFilter,这个过滤器是控制session并发问题

    <!-- SESSION并发处理 -->
	<bean id="concurrentSessionFilter"
		class="org.springframework.security.web.session.ConcurrentSessionFilter">
		<property name="sessionRegistry" ref="sessionRegistry" />
		<property name="expiredUrl" value="/error/timeout.jsp" />
		<property name="logoutHandlers">
			<list>
				<ref bean="logoutHandler" />
			</list>
		</property>
	</bean>

然后大致的过滤链就配置好了,对于cas等那些有需要用到的filter就自己看看源码,放到链条里就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 6中自定义权限过滤器的步骤如下: 1.创建一个类并实现`org.springframework.web.filter.OncePerRequestFilter`接口。 2.覆盖`doFilterInternal`方法,该方法接收`HttpServletRequest`和`HttpServletResponse`对象作为参数,并在其中编写自定义过滤器的逻辑。 3.使用`@Component`注释将自定义过滤器类标记为Spring组件。 4.在Spring Security配置类中使用`http.addFilterBefore()`方法将自定义过滤器添加到过滤中。 下面是一个示例代码,演示如何在Spring Security 6中创建自定义权限过滤器: ```java import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; @Component public class CustomAuthorizationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { // 在这里编写自定义过滤器的逻辑 // 检查用户是否有足够的权限访问请求的资源 // 如果没有权限,可以返回HTTP 403 Forbidden响应 // 如果有权限,可以继续处理请求 filterChain.doFilter(request, response); } } ``` 在Spring Security配置类中添加以下代码: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomAuthorizationFilter customAuthorizationFilter; @Override protected void configure(HttpSecurity http) throws Exception { http.addFilterBefore(customAuthorizationFilter, UsernamePasswordAuthenticationFilter.class) .authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .anyRequest().authenticated() .and() .formLogin() .and() .httpBasic(); } } ``` 在上面的示例中,我们创建了一个名为`CustomAuthorizationFilter`的自定义过滤器,并将其添加到Spring Security过滤中。在Spring Security配置类中,我们使用`http.addFilterBefore()`方法将自定义过滤器添加到过滤中,并使用`authorizeRequests()`方法配置了请求的授权规则。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值