我已设置自定义身份验证提供程序:
@Configuration
@EnableWebSecurity
@EnableGlobalAuthentication
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("samlAuthenticationProvider")
SAMLAuthenticationProvider samlAuthenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
/**
* Do your stuff here
*/
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(samlAuthenticationProvider);
}
}
现在,我想为身份验证管理器设置一个别名,然后我想在另一个bean定义中自动装配它。
例如:
<!-- Register authentication manager with SAML provider -->
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider
ref="samlAuthenticationProvider" />
</security:authentication-manager>
<!-- Processing filter for WebSSO Holder-of-Key profile -->
<bean id="samlWebSSOHoKProcessingFilter"
class="org.springframework.security.saml.SAMLWebSSOHoKProcessingFilter">
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationSuccessHandler" ref="successRedirectHandler" />
</bean>
有没有办法只在Java Config中这样做?
答案 0 :(得分:5)
我对新的安全Java配置不太满意,但这是我从源代码中看到的:
@Import(AuthenticationConfiguration.class)
public @interface EnableGlobalAuthentication {}
此注释也会AuthenticationConfiguration
导入@Configuration
。任何@Configuration
也被注册为bean。所以,你可以从WebSecurityConfigurerAdapter
:
@Autowired
public void setAuthenticationConfiguration(AuthenticationConfiguration authenticationConfiguration) {
this.authenticationConfiguration = authenticationConfiguration;
}
可以访问AuthenticationManager
:
this.authenticationConfiguration.getAuthenticationManager();
从xml的角度来看,您可以使用SpEL来访问authenticationManager
:
<property name="authenticationManager" value="#{authenticationConfiguration.authenticationManager}" />
抱歉,我没有看到AuthenticationManager
注册为bean的地方。从这里开始,您无法为他配置别名。
<强>更新强>
顺便说一句,如果您要@Autowired
AuthenticationManager
转到某个其他组件,@Value
会转到resque:
@Value("#{authenticationConfiguration.authenticationManager}")
private AuthenticationManager authenticationManager;
<强> UPDATE2 强>
找到它WebSecurityConfigurerAdapter
。源代码和JavaDocs:
/**
* Override this method to expose the {@link AuthenticationManager} from
* {@link #configure(AuthenticationManagerBuilder)} to be exposed as
* a Bean. For example:
*
* <pre>
* @Bean(name name="myAuthenticationManager")
* @Override
* public AuthenticationManager authenticationManagerBean() throws Exception {
* return super.authenticationManagerBean();
* }
* </pre>
*
* @return the {@link AuthenticationManager}
* @throws Exception
*/
public AuthenticationManager authenticationManagerBean() throws Exception {
return new AuthenticationManagerDelegator(authenticationBuilder);
}
<强> UPDATE3 强>
如何通过自定义AuthenticationManager
访问现有WebSecurityConfigurerAdapter
并配置SAMLWebSSOHoKProcessingFilter
?
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public SAMLWebSSOHoKProcessingFilter samlFilter() {
SAMLWebSSOHoKProcessingFilter samlFilter = new SAMLWebSSOHoKProcessingFilter();
samlFilter.setAuthenticationManage(authenticationManager());
.......
return samlFilter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilter(samlFilter());
}
}
答案 1 :(得分:0)
这对我有用:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
...
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
...
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
和
@Component
public class UsernamePasswordAuth extends UsernamePasswordAuthenticationFilter {
@Autowired
public UsernamePasswordAuth(AuthenticationManager authenticationManager) {
setAuthenticationManager(authenticationManager);
setFilterProcessesUrl("/api/services/login");
}
}