【问题标题】:How to convert spring security xml configuration to java configuration?如何将spring security xml配置转换为java配置?
【发布时间】:2015-07-22 13:39:00
【问题描述】:

我正在尝试将以下 xml 转换为 java 配置,但与 sec:authentication-manager 标记中的 alias 参数混淆。我已经完成了 java 配置的部分工作,如果有人可以帮助我完成其余的配置,我将不胜感激。

Xml 配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:sec="http://www.springframework.org/schema/security"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/security
                      http://www.springframework.org/schema/security/spring-security.xsd">

    <bean id="passwordEncoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"/>

    <sec:authentication-manager alias="userAuthenticationManager">
        <sec:authentication-provider user-service-ref="userService">
            <sec:password-encoder ref="passwordEncoder"/>
        </sec:authentication-provider>
    </sec:authentication-manager>

    <sec:authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
        <sec:authentication-provider user-service-ref="client-details-user-service"/>
    </sec:authentication-manager>


    <bean id="client-details-user-service" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
        <constructor-arg ref="client-details-service" />
    </bean>

</beans>

转换的部分

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private ClientDetailsService clientDetailsService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private Validator validator;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        // TODO Auto-generated method stub
        return super.authenticationManager();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.userDetailsService(userDetailsService());
    }


       @Bean
        public UserDetailsService userDetailsService() {
            return new UserServiceImpl(userRepository, validator, passwordEncoder);
        } 



}

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    alias 标签用于识别这个特定的身份验证管理器。如果您在java-config 中不需要这个,您可以尝试以下方法:

    @Autowired
    private StandardPasswordEncoder standardPasswordEncoder;
    
    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(principalService).passwordEncoder(standardPasswordEncoder);
        auth.authenticationProvider(preAuthenticatedAuthenticationProvider());
    }
    
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
       auth.userDetailsService(principalService).passwordEncoder(passwordEncoder);
       auth.authenticationProvider(preAuthenticatedAuthenticationProvider());
    }
    
    @Bean
    public PreAuthenticatedAuthenticationProvider preAuthenticatedAuthenticationProvider(){
        PreAuthenticatedAuthenticationProvider preAuthenticatedAuthenticationProvider = new PreAuthenticatedAuthenticationProvider();
        preAuthenticatedAuthenticationProvider.setPreAuthenticatedUserDetailsService(preAuthenticationUserDetailService);
        return preAuthenticatedAuthenticationProvider;
    }
    

    编辑

    如果您想要多重身份验证管理器,请查看以下答案: https://stackoverflow.com/a/26308661/1809221

    【讨论】:

    • 我只是为了在javaconfiguration中转换line to line,你能帮我在security config java文件中有两个身份验证管理器吗?
    【解决方案2】:

    Java 配置可以和 XML 中的命名空间配置一样。

    我建议你在@Configuration 类中遵循以下规则:

    • 在 xml 中用 &lt;bean ...&gt; 声明的所有内容都应该是 java 配置中的 @Bean
    • 在别处定义并在此 xml 中使用的所有 bean 都应该是 @Autowired

    由于您在 xml 中有 2 个不同的身份验证管理器,您应该在 Java 配置中显式创建它们,因为 @ManuZi 引用的 other question 表明,如果您尝试通过基本配置配置 2 个不同的 AuthenticationManager,springSecurityFilterChain 的实例化将失败java配置,你最好使用带有@Qualifier注解的显式注入。

    【讨论】:

      猜你喜欢
      • 2015-05-30
      • 2011-01-15
      • 1970-01-01
      • 2016-10-24
      • 2014-04-06
      • 1970-01-01
      • 2018-07-19
      • 2015-11-19
      • 1970-01-01
      相关资源
      最近更新 更多