小编io-*_*man的帖子

Spring Security 3.2基于代码的配置.表单登录+全局方法安全性

我正在我的项目中将Spring安全性从3.1升级到3.2.由于3.2支持基于代码的配置,因此我决定将旧的基于XML的配置转换为Java代码.

我收到异常说"authenticationManager"每次尝试启动应用程序时都没有找到带名字的bean .@EnableGlobalMethodSecurity注释添加到配置类后,此异常开始出现.

Spring框架版本:4.0.0.RELEASE

Spring安全版:3.2.0.RELEASE

旧的Xml配置看起来像这样:

<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-3.2.xsd
                                 http://www.springframework.org/schema/security
                                 http://www.springframework.org/schema/security/spring-security-3.1.xsd">

  <global-method-security secured-annotations="enabled"
                        jsr250-annotations="enabled"
                        pre-post-annotations="enabled"
                        proxy-target-class="true"/>

  <http auto-config="true" use-expressions="true" >
    <form-login login-page="/login" 
                default-target-url="/home" 
                authentication-failure-url = "/login?login_error=1" />

    <logout logout-url="/logout" logout-success-url="/index" />
  </http>

  <authentication-manager>
    <authentication-provider user-service-ref="authUserDetailService">
      <password-encoder hash="plaintext"/>
    </authentication-provider>
  </authentication-manager>
</beans:beans>
Run Code Online (Sandbox Code Playgroud)

新的Java配置类看起来像这样:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER").and()
                .withUser("admin").password("password").roles("USER", "ADMIN");
    }

    @Bean
    public UserDetailsService userDetailsServiceBean() …
Run Code Online (Sandbox Code Playgroud)

spring-security spring-java-config

6
推荐指数
1
解决办法
1万
查看次数