Spring Boot + Security + Multi HTTP Web Configuration

后端 未结 3 1044
礼貌的吻别
礼貌的吻别 2021-02-06 08:54

I\'m trying to do an example using spring-boot with spring security. My idea is to create a web app and also provide an API, I would like to both have security; so I need to cr

相关标签:
3条回答
  • 2021-02-06 09:31

    I found I could solve this problem by annotating my class with @EnableWebSecurity after reading this hint: https://github.com/spring-projects/spring-data-examples/issues/189#issuecomment-229552207

    0 讨论(0)
  • 2021-02-06 09:35

    after a lot of reading I found something that works for me:

    @Configuration
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    @EnableGlobalMethodSecurity(securedEnabled = true)
    public class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {
    
        @Resource(name = "customUserDetailsService")
        protected CustomUserDetailsService customUserDetailsService;
    
        @Resource
        private DataSource dataSource;
    
        @Autowired
        protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(customUserDetailsService);
        }
    
        @Configuration
        @Order(1)
        public static class ApiConfigurationAdapter extends WebSecurityConfigurerAdapter {
            @Resource(name = "restUnauthorizedEntryPoint")
            private RestUnauthorizedEntryPoint restUnauthorizedEntryPoint;
            @Resource(name = "restAccessDeniedHandler")
            private RestAccessDeniedHandler restAccessDeniedHandler;
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> securityXAuthConfigurerAdapter = new XAuthTokenConfigurer(
                        userDetailsServiceBean());
    
                // @formatter:off
                http
                    .antMatcher("/api/**").csrf().disable()
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .exceptionHandling()
                        .authenticationEntryPoint(restUnauthorizedEntryPoint)
                        .accessDeniedHandler(restAccessDeniedHandler)
                    .and()
                        .authorizeRequests()
                            .antMatchers(HttpMethod.POST, "/api/authenticate").permitAll()
                            .anyRequest().hasRole("ADMIN")
                            .and()
                            .apply(securityXAuthConfigurerAdapter);
                // @formatter:on
            }
        }
    
        @Configuration
        @Order(2)
        public static class WebConfigurationAdapter extends WebSecurityConfigurerAdapter {
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                // @formatter:off
                http
                    .authorizeRequests()
                        .antMatchers("/", "/home").permitAll()
                        .anyRequest().authenticated()
                        .and()
                        .formLogin()
                            .loginPage("/login").permitAll()
                        .and()
                        .logout().permitAll()
                ;
                // @formatter:on
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-06 09:42

    I'm also faced the same issue. But I got it solved when I extend the WebSecurityConfiguration master class from WebSecurityConfigurerAdapter.

    Kindly refer the following stackoverflow post in which you can find the full configuration.

    Spring Security HTTP Basic for RESTFul and FormLogin for web - Annotations

    0 讨论(0)
提交回复
热议问题