首页 > 解决方案 > 为什么大摇大摆地忽略我的身份验证?

问题描述

我正在尝试使用 j OpenId Connect 保护一个招摇接口。

我可以使用 OIDC 登录,并且 swagger 向我显示授权:

在此处输入图像描述 但是当我“尝试”时,身份验证被忽略并弹出一个登录框:

![在此处输入图像描述

在我的课堂上extends SpringBootServletInitializer

   @Bean
   @ConditionalOnProperty("security.oauth2.client.clientId") 
    public SecurityScheme securityScheme(Environment environment, OAuth2ClientProperties clientProperties) {
        String authorizationUri = environment.getRequiredProperty("security.oauth2.client.user-authorization-uri");
        String accessTokenUri = environment.getRequiredProperty("security.oauth2.client.access-token-uri");

        LoginEndpoint loginEndpoint = new LoginEndpoint(authorizationUri);
        TokenRequestEndpoint tokenRequestEndpoint =
                new TokenRequestEndpoint(authorizationUri, clientProperties.getClientId(), clientProperties.getClientSecret());
        TokenEndpoint tokenEndpoint = new TokenEndpoint(accessTokenUri, "auth_code");
        GrantType grantType = new AuthorizationCodeGrant(tokenRequestEndpoint, tokenEndpoint);
        AuthorizationScope authorizationScope = new AuthorizationScope(authorizationScopeGlobal, authorizationScopeGlobal);
        return new OAuthBuilder()
                .name(securitySchemaOAuth2)
                .grantTypes(Arrays.asList(grantType))
                .scopes(Arrays.asList(authorizationScope))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope(authorizationScopeGlobal, authorizationScopeGlobalDesc);
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference(securitySchemaOAuth2, authorizationScopes));
    }

    @Bean
    SecurityConfiguration security(OAuth2ClientProperties clientProperties) {
        return new SecurityConfiguration(
                clientProperties.getClientId(),
                clientProperties.getClientSecret(),
                securitySchemaOAuth2,
                "test-app",
                "apiKey",
                ApiKeyVehicle.HEADER, 
                "api_key",
                " " /*scope separator*/);
    }

    @Bean
    public SecurityContext securityContext() {
        return SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("/v1/.*")).build();
    }

和一个类:

 @ApiModel(value = "Template", description = "Template of REST APIs")
@RestController
@RequestMapping("/v1")
public class TemplateServiceImplementation {

...

@ApiOperation(httpMethod = "GET", value = "Call Get method",
        notes = "See Get method")
@RequestMapping(method = RequestMethod.GET, value = "/calltemplate/{param}/", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Structure> callGet(@PathVariable("param") String param, HttpServletRequest hreq) {

    MultiValueMap<String, String> mapParams = new LinkedMultiValueMap<String, String>();
    mapParams.add("param", param);
    Structure structure = restTemplate.getForObject(callGetEndpoint, Structure.class, mapParams);

    ResponseEntity<Structure> thisresponse = new ResponseEntity<Structure>(structure, HttpStatus.OK);
    return thisresponse;
}

对不起所有的代码。那么如何让 GET 使用我的 OIDC 身份验证呢?

当我取消登录时,curl 是: curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer eyJraWQiOiJyc2ExIiwiYWxnIjoiUlMy lots more encrypted text' 'http://localhost:8080/v1/calltemplate/%7B%20%20%20%22id%22%3A%20%22string%22%2C%20%20%20%22name%22%3A%20%22string%22%2C%20%20%20%22path%22%3A%20%22string%22%2C%20%20%20%22version%22%3A%20%22string%22%20%7D/' request url: http://localhost:8080/v1/calltemplate/%7B%20%20%20%22id%22%3A%20%22string%22%2C%20%20%20%22name%22%3A%20%22string%22%2C%20%20%20%22path%22%3A%20%22string%22%2C%20%20%20%22version%22%3A%20%22string%22%20%7D/

和其他响应变量:

在此处输入图像描述

编辑忘记提及我的WebSecurityConfig

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", 
            "/swagger-resources/**", "/configuration/**", "/swagger-ui.html"
            , "/webjars/**", "/csrf", "/");
}

更新网络输出: 在此处输入图像描述

标签: spring-bootswagger-uiopenid-connect

解决方案


推荐阅读