【发布时间】:2014-01-24 13:02:48
【问题描述】:
我想使用custom authentication filter:
- 捕获加密的标头令牌
- 验证后,提取用户的详细信息并以无状态的方式将它们添加到当前请求的安全上下文中
我希望能够使用此安全上下文持有者来获取有关当前请求用户正确处理其请求的详细信息。
@RequestMapping(value = "/simple", method = RequestMethod.POST)
@ResponseBody
@Transactional
@Preauthorize(...)
public String simple(){
//collect the user's current details from the getPrinciple() and complete the transaction...
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return "Simple";
}
我以前在 XML 中这样做过:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<security:global-method-security
secured-annotations="enabled" />
<security:http pattern="/**"
auto-config="true" disable-url-rewriting="true" use-expressions="true">
<security:custom-filter ref="authenticationTokenProcessingFilter"
position="FORM_LOGIN_FILTER" />
<security:intercept-url pattern="/authenticate"
access="permitAll" />
<security:intercept-url pattern="/secure/**"
access="isAuthenticated()" />
</security:http>
<bean id="CustomAuthenticationEntryPoint" class="org.foo.CustomAuthenticationEntryPoint" />
<bean class="org.foo.AuthenticationTokenProcessingFilter" id="authenticationTokenProcessingFilter">
<constructor-arg ref="authenticationManager" />
</bean>
</beans>
但是,我希望它可以在非 xml WebSecurityConfigurerAdapter 中与较新的Spring Boot 应用程序一起使用,就像他们的 Spring Boot 文件中的示例一样:
@Bean
public ApplicationSecurity applicationSecurity() {
return new ApplicationSecurity();
}
@Order(Ordered.LOWEST_PRECEDENCE - 8)
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// this is obviously for a simple "login page" not a custom filter!
http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin()
.loginPage("/login").failureUrl("/login?error").permitAll();
}
}
有什么建议或类似的例子吗?
【问题讨论】:
标签: spring authentication spring-security