当前位置: 首页 > 知识库问答 >
问题:

Spring Security OAuth2和FormLogin在一个应用程序中

公良扬
2023-03-14

在我的Spring Boot应用程序中,我有RESTful API和用于管理的MVC web仪表板。

是否可以在一个应用程序中同时为RESTful API提供Spring Security OAuth2身份验证/授权(基于令牌的、无状态的)和为Spring MVC web仪表板提供FormLogin(有状态的)?

如何用Spring Boot正确配置它?

共有1个答案

宣煜
2023-03-14

您需要为基于表单的登录和资源服务器安全表单RESTendpoint配置web安全性

下面是一个工作配置,它使用单独部署的授权服务器进行单点登录

@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;
    }
}
 类似资料:
  • 问题内容: 我确信你们中有人注意到,如果您有Acrobat Reader(或其他PDF阅读器),并在Firefox中打开一个PDF,您会看到它嵌入在您的标签中。有什么方法可以将应用程序嵌入JFrame中? 问题答案: 这是一个相当棘手的问题。通常,诸如Adobe Reader之类的本机应用程序不提供可以嵌入到swing应用程序中的组件。但是在Windows中,有COM / OLE方法可以将应用程序

  • 我正在开发两个应用程序。让第一个应用程序是APP1,第二个应用程序为APP2。现在在APP1中,我不提供任何用户权限,如INTERNET权限,但它将发送任何http url,如http://www.google.com我的第二个APP2将包含INTERNET等用户权限。Http请求将从APP1发送到APP2,APP2将响应该请求,然后将结果发送回APP1。最后APP1包含一个Web视图以显示结果。

  • 我在google play商店里有两个应用程序。有可能在第一个描述中创建一个从一个到另一个的http链接吗?

  • 问题内容: 我想通过JDBC连接到两个不同的Oracle数据库(一个8.0.5.0.0和一个12c)。我确实有两个JDBC驱动程序,它们可以通过简单的“ hello world”应用程序分别成功地连接到相应的DB。下面,我将它们都放在一个Java应用程序中,不幸的是,该应用程序不再起作用(加载了两个驱动程序)。 我已经阅读了这篇文章:从SAMEVENDOR处理多个JDBC驱动程序。提到的选项1可能

  • 我有两个应用程序:同事和服务,每个都有自己的模型 在coworkers models.py中,我可以“从services.models导入服务”。 当我尝试在services models.py中“from coworkers.models import Status”时,会收到以下错误消息: 回溯(最近一次调用):文件“/Users/lucas/Documents/projetos/cwk-ma

  • 问题内容: 我正在为我的游戏创建一个关卡编辑器,但在将LwjglCanvas与JFrame一起使用时遇到了问题。我使用JFrame(而不是LwjglFrame)来保持引擎和关卡编辑器尽可能独立。我有两个JAR:WorldEditor.jar和GameEngine.jar。在WorldEditor内部,我有一个名为“测试”的按钮,该按钮假设是将GameEngine.jar(如果尚未加载)加载并将其启