我有一个应用程序,它从java类加载默认的spring安全配置,并可选择从外部文件导入添加配置:
@Configuration
@EnableWebSecurity
@EnableAspectJAutoProxy
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
@ImportResource("file:///${my.home.dir}\\conf\\security.xml")
public class WebSecurityConfiguration implements Serializable {
private static final long serialVersionUID = 6654777887140629668L;
@Configuration
@Order
public static class AnotherWebSecurityConfiguration extends WebSecurityConfigurerAdapter implements Serializable {
private static final long serialVersionUID = 4628321113541373781L;
public AnotherWebSecurityConfiguration() {
super();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(ignoredUrls); // we want to disable Spring Security for static resources.
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests().antMatchers("/**").hasRole("USER").and().httpBasic().and().csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
}
外部文件包含以下内容:
<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-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<!-- enable use-expressions -->
<http auto-config="true" pattern="/manage/app/**" authentication-manager-ref="anotherAuthenticationManager" use-expressions="true">
<intercept-url pattern="/manage/app/**" access="hasRole('ROLE_USER')" />
<http-basic />
</http>
<authentication-manager id="anotherAuthenticationManager">
<authentication-provider>
<user-service>
<user name="admin" password="admin2" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
所以基本上我说的是我需要有2个身份验证访问点:
/ **和/ manage / app / **
当我执行应用程序时,我收到以下错误:
期望只找到类型接口的单个bean org.springframework.security.authentication.AuthenticationManager但是 3找到了......
所以我将以下方法添加到主类WebSecurityConfiguration
中protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication();
}
在这种情况下,xml安全配置工作正常,但java配置被忽略了。
请告诉我在配置中做错了什么?
请注意,我使用的是spring security 3.2
非常感谢。
答案 0 :(得分:0)
XML配置会覆盖java配置 - https://jira.spring.io/browse/SPR-7341。我没有看到你想要混合这些的原因,现在花一些时间重写这将节省你的未来:)你可以使用http.antMatchers(...)和()合并两个配置。