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

在spring中实现授权代码授予时的问题

李星波
2023-03-14
@Configuration
@PropertySource("classpath:webservices-application.properties")
@EnableResourceServer
public class ResourceServer extends ResourceServerConfigurerAdapter{

    @Value("${security.oauth2.resource.id}")
    private String resourceId;

    @Bean
    public JdbcTokenStore getTokenStore() {
        return new JdbcTokenStore(dataSource);
    }

    @Autowired
    private DataSource dataSource;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/oauth/**","/login","/").permitAll()
                .anyRequest().authenticated();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.tokenStore(getTokenStore())
                .resourceId(resourceId).stateless(false);
    } 
} 
@Configuration
@EnableWebSecurity
@EnableOAuth2Sso
public class CustomWebsecurity extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/oauth/**","/login","/").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin();
    }
} 

AuthorizationServer:

@Configuration
@EnableAuthorizationServer
@EnableOAuth2Sso
protected class AuthorizationApplication extends AuthorizationServerConfigurerAdapter {

    @Autowired
    public AuthorizationApplication (ApplicationContext applicationContext, AuthenticationManager authenticationManager) {
        this.passwordEncoder = applicationContext.getBean(PasswordEncoderImpl.class);
        this.authenticationManager = authenticationManager;
    }

    private PasswordEncoder passwordEncoder;

    private AuthenticationManager authenticationManager;

    @Bean
    protected AuthorizationCodeServices getAuthorizationCodeServices() {
        return new JdbcAuthorizationCodeServices(dataSource);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        AuthorizationCodeServices services = getAuthorizationCodeServices();
        JdbcTokenStore tokenStore = getTokenStore();
        endpoints
                .userDetailsService(userDetailsService)
                .authorizationCodeServices(services)
                .authenticationManager(authenticationManager)
                .tokenStore(tokenStore)
                .approvalStoreDisabled();
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients();
        security.passwordEncoder(passwordEncoder);
    }
}

该问题可能是由于WebSecurity类的某些配置不正确造成的。但是,我尝试了多种配置,但都没有运气。

共有1个答案

容俊豪
2023-03-14

在@dur的一些指导下,我找到了解决方案。这里有一个罪魁祸首:

OAuth2资源筛选器的默认顺序已从3更改为SecurityProperties.Access_Override_Order-1。这将它放置在执行器endpoint之后,但在基本身份验证筛选器链之前。可以通过设置security.oauth2.resource.filter-order=3来恢复默认值

总而言之,我做了以下改动:

java.lang.IllegalArgumentException: URI must not be null

通过将以下内容放入属性文件,更改资源筛选器的筛选顺序:

security.oauth2.resource.filter-order = 3

安全配置中的一些基本更改。

下面是我的ResourceServer类:

@Configuration
@PropertySource("classpath:webservices-application.properties")
@EnableResourceServer
@EnableOAuth2Sso
public class ResourceServer extends ResourceServerConfigurerAdapter{

    @Value("${security.oauth2.resource.id}")
    private String resourceId;

    @Bean
    public JdbcTokenStore getTokenStore() {
        return new JdbcTokenStore(dataSource);
    }

    @Autowired
    private DataSource dataSource;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .requestMatchers().antMatchers(
                "/protected_uri_1",
                "/protected_uri_2",
                "/protected_uri_3")
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
        .and().formLogin();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.tokenStore(getTokenStore())
                .resourceId(resourceId);
    }

}
 类似资料:
  • 我们必须在我的客户项目中集成OAuth2.0授权代码授予。目前,该应用程序使用本地登录页面。我们需要删除该页面,并将未登录的用户重定向到AS登录页面,。在AS end成功登录后,我们将被重定向到配置的。此时,我的客户端应用程序将如何知道用户已在AS登录?如何在客户端维护会话?另外,我需要用和访问令牌交换auth代码,并将其用于后续的服务器API调用。那么如何实现这一点并将令牌作为标头发送呢? 应用

  • 本文向大家介绍oauth 授权码授予,包括了oauth 授权码授予的使用技巧和注意事项,需要的朋友参考一下 示例 第1步 第2步 资源

  • 产品-服务:具有受保护路由的简单下游服务 jwt-resoure-server:当包含在下游服务中时,使其成为提取jwt令牌并将其设置在安全contex中的资源服务器的jar。 尤里卡-服务:发现服务 zuul-server:边缘服务器 Okta是我的auth服务器 我已经将oauth grant类型设置为be-Authorization code(我知道对于spa来说,建议使用隐式grant类型

  • 我是这方面的新手,读了很多之后,我觉得我不太明白如何在Keycloak中实现授权代码流。我的疑惑: > < li> 在创建了支持此流程的客户端后,如何执行凭据验证?默认情况下,如果我不做任何配置,我会得到一个登录表单。如果我在浏览器中打开这个html,并填写用户和密码字段,当我按下按钮时,它会将我发送到一个类型为.../realm/{ REAL _ NAME }/log in-actions/au

  • 对于授权码授予,密钥是否应该映射到每个单独的用户? 发送电子邮件时,发件人的电子邮件是什么? 谢谢 J Larry的其他信息: 这太棒了!。Docusign对库存流进行了积极监控!我正在使用JWT 我配置了一个用户。我假设电子邮件正文将类似于下面的一个。 我的问题是,如果使用密钥来识别应用程序,客户知道电子邮件是从应用程序发送的,但他如何知道使用应用程序的哪个员工发送电子邮件? 请参阅以下电子邮件

  • 我试图复制cognito hosted ui sign in页面(https://docs.aws.amazon.com/cognito/latest/developerguide/login-endpoint.html)的功能,但是aws sdk没有提供任何获取用户名、密码并返回授权代码的方法,这些代码可用于获取Access/ID令牌。initiateAuth和adminInitiateAuth