带有aws cognito的春季项目

时间:2020-08-31 07:48:54

标签: spring amazon-web-services amazon-cognito

我正在尝试在我的spring项目中使用aws Cognito的用户身份验证。我配置了Cognito并从中获得了jwt令牌。当我将令牌传递给api端点时,出现错误401(未经授权)。

我正在工作在本地主机上的ms。

我看到了一些示例,我觉得我的application.properties缺少了一些东西,但是我不确定是什么。

我的应用程序属性:

spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://cognito-idp.eu-west-2.amazonaws.com/eu-west-2_somecode
spring.security.oauth2.client.registration.app1.client-id=my_app_id_in_cognito

我的pom具有以下安全依赖性:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-oauth2-resource-server</artifactId>
    <version>5.3.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-oauth2-jose</artifactId>
    <version>5.3.3.RELEASE</version>
</dependency>

我的配置类:

   @Configuration
   public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .anyRequest().authenticated().and()
                .oauth2ResourceServer().jwt();
    }

还尝试根据某些spring examples更改应用程序属性:

spring.security.oauth2.client.registration.cognito.client-id=my_app_id_in_cognito
spring.security.oauth2.client.registration.cognito.client-name=app1
spring.security.oauth2.client.provider.cognito.issuer-uri=https://cognito-idp.eu-west-2.amazonaws.com/eu-west-1_somecode

但是随后出现以下异常,因为我缺少application.properties中的spring.security.oauth2.resourceserver.jwt.jwk-set-uri设置:

Method springSecurityFilterChain in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' that could not be found.

The following candidates were found but could not be injected:
    - Bean method 'jwtDecoderByIssuerUri' in 'OAuth2ResourceServerJwtConfiguration.JwtDecoderConfiguration' not loaded because OpenID Connect Issuer URI Condition did not find issuer-uri property
    - Bean method 'jwtDecoderByJwkKeySetUri' in 'OAuth2ResourceServerJwtConfiguration.JwtDecoderConfiguration' not loaded because @ConditionalOnProperty (spring.security.oauth2.resourceserver.jwt.jwk-set-uri) did not find property 'spring.security.oauth2.resourceserver.jwt.jwk-set-uri'
    - Bean method 'jwtDecoderByPublicKeyValue' in 'OAuth2ResourceServerJwtConfiguration.JwtDecoderConfiguration' not loaded because Public Key Value Condition did not find public-key-location property


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' in your configuration.

我想念什么?

1 个答案:

答案 0 :(得分:0)

我找到了this article,并尝试在application.properties中使用相同的设置:

<asp:Label ID="lbProbType" runat="server" Text="Problem Type"" 
Font-Bold="True"></asp:Label>



if (!IsPostBack)
            {
                SqlConnection cn = new SqlConnection(@"");
                string readnamesquery = "select cwFullTitle from tbCowWorkers order by cwLName";
                cn.Open();
                SqlCommand cmd = new SqlCommand(readnamesquery, cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                
                DataTable dt = new DataTable();
                da.Fill(dt);
               
                dpCaller.DataSource = dt;
                dpCaller.DataTextField = "cwFullTitle";
                dpCaller.DataBind();
                cn.Close();
            }
相关问题