具有弹簧启动配置的弹簧安全性

时间:2014-06-24 14:42:26

标签: spring-security spring-boot

我有一个使用Spring-security的Spring-boot应用程序,配置了Java-config。理想情况下,我将有一个客户UserDetailsS​​ervice,以便我可以添加/修改用户。在那之前,我无法正确配置。

我使用以下依赖项:

compile("org.springframework.boot:spring-boot-starter-web:1.1.1.RELEASE")
compile("org.springframework.boot:spring-boot:1.0.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.1.1.RELEASE")
compile("org.springframework.security:spring-security-web:4.0.0.M1")
compile("org.springframework.security:spring-security-config:4.0.0.M1")

我有以下配置

@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@Configuration
@EnableWebMvcSecurity
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource datasource;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/css/**").permitAll();

        http
                .formLogin().failureUrl("/login?error")
                .defaultSuccessUrl("/")
                .loginPage("/login")
                .permitAll()
                .and()
                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/")
                .permitAll();

        http
                .authorizeRequests().anyRequest().authenticated();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        JdbcUserDetailsManager userDetailsService = jdbcUserService();
//        userDetailsService.setDataSource(datasource);
//        PasswordEncoder encoder = new BCryptPasswordEncoder();

        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
        auth.jdbcAuthentication().dataSource(datasource);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public org.springframework.security.provisioning.JdbcUserDetailsManager jdbcUserService() throws Exception {
        JdbcUserDetailsManager jdbcUserDetailsManager = new JdbcUserDetailsManager();
        jdbcUserDetailsManager.setDataSource(datasource);
        jdbcUserDetailsManager.setAuthenticationManager(authenticationManagerBean());
        return jdbcUserDetailsManager;
    }
}

@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
public class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .jdbcAuthentication()
                .dataSource( dataSource );
    }
}

所以,我意识到我的配置错误但不确定如何最好地修复它们。症状是,当我登录我的Thymeleaf UI时,会话永远不会过期。

我已经使用各种在线资源进行春季安全学习和学习。实现。不幸的是,我仍然没有理解为什么这不正确。

1 个答案:

答案 0 :(得分:0)

您似乎正在配置3个过滤器链(3 WebSecurityConfigurerAdapters),但只有其中一个配置HttpSecurity。这可能不是你打算做的。也许可以合并到WebSecurityConfigurerAdapter和一个GlobalAuthenticationConfigurerAdapter并查看它的位置。