【问题标题】:Can Spring Boot application have separate security for REST APIs?Spring Boot 应用程序可以为 REST API 提供单独的安全性吗?
【发布时间】:2015-03-06 23:37:24
【问题描述】:

我们希望为 Rest Controller 应用基于 Oauth2 的安全性,而应用程序的其余部分将具有 Spring Security。这可能吗?你能提供任何例子吗?

WebSecurityConfigurerAdapter 和 ResourceServerConfigurerAdapter 似乎在两者都配置时发生冲突。

提前谢谢你。

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    是的,有可能。这里给出了示例模板配置代码。请根据您的需要更改所需的配置。 关键是用不同的顺序定义配置的子静态类。在这里,我将来自\api 的任何请求视为REST API 调用。

    我没有通过编译来检查代码。

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Order(1)
        @Configuration
        public static class ApiWebSecurityConfig extends OAuth2ServerConfigurerAdapter{
    
            @Override
            protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                //Write the AuthenticationManagerBuilder codes for the OAuth
            }
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable()
                        .antMatcher("/api/**")
                        .authorizeRequests()
                            .anyRequest().authenticated()
                            .and()
                        .apply(new OAuth2ServerConfigurer())
                        .tokenStore(new InMemoryTokenStore())
                        .resourceId(applicationName);
                }
            }
        }
    
        @Order(2)
        @Configuration
        public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{
    
            @Autowired
            public void configure(AuthenticationManagerBuilder auth) throws Exception {
                //Write the AuthenticationManagerBuilder codes for the Normal authentication
            }
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable() //HTTP with Disable CSRF
                        .authorizeRequests() //Authorize Request Configuration
                            .anyRequest().authenticated()
                            .and() //Login Form configuration for all others
                        .formLogin()
                            .loginPage("/login").permitAll()
                            .and() //Logout Form configuration
                        .logout().permitAll();
            }
        }
    }
    

    【讨论】:

    • 不需要静态类。即使您将它们放在单独的文件中,@Order 注释也有效。
    • @HajderRabiee 是的,它肯定可以工作。 :) 这是我的编码风格。你可以拥有自己的。
    • @FarajFarook 我收回了 :) 当我有两个单独的类(2 个 .java 文件)时,我的应用程序似乎没有选择两个不同的身份验证提供程序。我目前有两个静态子类。稍后会深入研究这个问题,或者如果有人可以确认/否认这一事实。
    • 在我的应用程序中,我有两个单独的类及其工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2016-06-08
    • 2021-11-21
    • 2019-09-20
    • 2019-07-29
    • 2021-06-17
    相关资源
    最近更新 更多