spring安全配置xml到java
有人能帮我从基于xml的spring配置迁移到基于java的spring配置吗 以下是我的xml配置:spring安全配置xml到java,java,xml,spring,Java,Xml,Spring,有人能帮我从基于xml的spring配置迁移到基于java的spring配置吗 以下是我的xml配置: <!--suppress SpringFacetInspection, SpringSecurityFiltersConfiguredInspection --> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.sp
<!--suppress SpringFacetInspection, SpringSecurityFiltersConfiguredInspection -->
<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"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<global-method-security pre-post-annotations="enabled"/>
<context:annotation-config/>
<context:spring-configured/>
<beans:bean name="userLoginService" class="service.UserLoginService"/>
<beans:bean name="standardPasswordEncoder"
class="org.springframework.security.crypto.password.StandardPasswordEncoder">
<beans:constructor-arg name="secret" value="supersecret"/>
</beans:bean>
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/javax.faces.resources/**" access="permitAll"/>
<intercept-url pattern="/view/unsecured/**" access="permitAll"/>
<intercept-url pattern="/view/secured/**" access="isAuthenticated()" />
<intercept-url pattern="/view/admin/**" access="hasRole('ROLE_SUPERUSER')"/>
<intercept-url pattern="/admin/**" access="hasRole('ROLE_SUPERUSER')"/>
<form-login login-page="/view/unsecured/login.xhtml"/>
<logout logout-success-url="/index.xhtml" invalidate-session="true" delete-cookies="true"/>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userLoginService">
<password-encoder ref="standardPasswordEncoder"/>
</authentication-provider>
</authentication-manager>
</beans:beans>
当我尝试登录时,出现异常
Caused by: java.lang.StackOverflowError: null
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:386)
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:387)
...
我错过了什么,还是怎么了?
谢谢重写
userDetailsService()
方法,而不是userDetailsServiceBean()
方法。这就是导致无限递归的原因。那么就不需要将其声明为@Bean
应该是:
@Override
public UserDetailsService userDetailsService() {
return new UserLoginService();
}
或者-如果在
UserLoginService
上有@Service
注释(以及将获取该类的组件扫描),则可以避免手动创建bean,只需将UserLoginService
直接注入配置方法:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, UserLoginService userDetailsService) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(standardPasswordEncoder());
}
这样你就不需要重写
userDetailsServiceBean
或userDetailsService
:因为他们所做的就是创建该bean的实例。谢谢,这对我很有帮助。我还有一个问题。在单击login按钮后,使用XMLConfig,我在ManagedBean中使用了我的loginAction()。现在这个操作没有被调用,但是我可以看到UserLoginService被调用了。你知道如何再次调用我的自定义操作吗?好的,我成功了。我已经删除了.loginPage(“/view/unsecured/login.xhtml”).usernameparter(“email”).passwordParameter(“password”)
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, UserLoginService userDetailsService) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(standardPasswordEncoder());
}