Spring Security OAuth2 and FormLogin in a one application

后端 未结 1 1561
栀梦
栀梦 2020-12-18 16:00

In my Spring Boot application I have RESTful API and MVC web dashboard for administration.

Is it possible to have both - Spring Security OAuth2 authentication/author

1条回答
  •  醉梦人生
    2020-12-18 16:33

    You need to configure your web security for form based login and Resource Server Security form REST Endpoints

    Here is a working configuration that uses single sign on with an Authorization Server deployed separately.

    @Configuration
    @EnableOAuth2Sso
    @EnableWebSecurity
    protected static class ResourceConfiguration extends WebSecurityConfigurerAdapter {
    
        @Value("${sso.url}")
        private String ssoUrl;
    
        @Autowired
        private  RedisConnectionFactory redisConnectionFactory;
    
        @Bean
        protected TokenStore tokenStore() {
            return new RedisTokenStore(redisConnectionFactory);
        }
    
        @Bean
        @Primary
        protected ResourceServerTokenServices tokenServices() {
            DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
            defaultTokenServices.setTokenStore(tokenStore());
            defaultTokenServices.setSupportRefreshToken(true);
    
            return defaultTokenServices;
        }
    
    
        @Override
        @Bean
        public AuthenticationManager authenticationManagerBean() throws Exception {
            OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager();
            authenticationManager.setTokenServices(tokenServices());
            return authenticationManager;
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {      
            http.requestMatchers()
            .and().authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers(HttpMethod.GET, "/static/**").permitAll()
                .antMatchers(HttpMethod.GET, "/profile/**").permitAll()
                .antMatchers(HttpMethod.GET, "/services/**").permitAll()
                .anyRequest().authenticated()
            .and().logout()
                    .invalidateHttpSession(true)
                    .logoutSuccessUrl(ssoUrl+"/logout")
                    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                    .deleteCookies("JSESSIONID").invalidateHttpSession(true)
                    .permitAll();
        }
    
    }
    
    @Configuration
    @EnableResourceServer
    @Order(1)
    protected static class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    
    
    
        @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources.resourceId("resource-id");
        }
    
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.requestMatcher(new OAuthRequestedMatcher())
                .authorizeRequests().anyRequest().fullyAuthenticated();
    
        }
    }
    
    private static class OAuthRequestedMatcher implements RequestMatcher {
        public boolean matches(HttpServletRequest request) {
            String auth = request.getHeader("Authorization");
            boolean haveOauth2Token = (auth != null) && auth.startsWith("Bearer");
            boolean haveAccessToken = request.getParameter("access_token")!=null;
            return haveOauth2Token || haveAccessToken;
        }
    }
    

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