编程语言
首页 > 编程语言> > 使用java配置的spring security custom-filter

使用java配置的spring security custom-filter

作者:互联网

如何在java配置中使用自定义过滤器替换默认过滤器?在XML中,它将是,例如:

<bean id="myFilter" class="lalalal.MyFilter">
<property name="authenticationManager" ref="authenticationManager"/>
</bean>

<security:http auto-config="true">     
      <security:custom-filter ref="myFilter" position="FORM_LOGIN_FILTER"/>
</security:http> 

关于filterBefore,filterAfter和默认过滤器我知道.

解决方法:

假设您对Spring安全性的Java配置有一般的了解,添加过滤器相对简单(general details of updating spring-security config to java here):

因此,在WebSecurityConfigurerAdapter实现中,执行以下操作:

@Configuration
@EnableWebSecurity
class SecurityConfiguration extends WebSecurityConfigurerAdapter {

   @Override protected void configure(HttpSecurity http) throws Exception {

       //Custom security filters
       http.addFilterBefore( myFilter(), BasicAuthenticationFilter.class );

       //Rest of the security configuration goes here
       ...
   }

这是一个非常简单的例子 – 但希望有足够的帮助 – 你可以在这里添加额外的安全配置(例如基于角色的限制,csrf,会话配置等)和myFilter()是另一种定义你在设置的问题中提到的bean的方法你的过滤器.还有一个addFilterAfter()方法,您可以选择将过滤器放在链中的位置.

Here is an example for API security that shows some custom filters being used.

标签:spring,spring-security,servlet-filters,spring-java-config
来源: https://codeday.me/bug/20190708/1401006.html