Hier ist eine minimale Version des Codes:
MyApplication.java
Code: Select all
@SpringBootApplication
@EnableOAuth2Sso // this enables the authorization code flow via sso/pwd
@RestController
@Order(value = 0) // makes this security adapter have precedence over others
public class MyApplication extends WebSecurityConfigurerAdapter {
private final Logger log = LoggerFactory.getLogger(this.getClass());
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@RequestMapping(value = "/userinfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public void getUserInfo(final @AuthenticationPrincipal OAuth2Authentication activeUser) throws Exception {
log.info("GET /userinfo called");
activeUser.getAuthorities();
log.info("Principal: "+activeUser.getPrincipal());
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
//Added by me, I've tried with and without this line with no difference to the result
//http.oauth2ResourceServer()
// .jwt()
// .jwtAuthenticationConverter(jwtAuthenticationConverter());
// Minimal http block to run server, the original is much longer but also results in empty Principals
http.csrf().disable()
.authorizeRequests().antMatchers("/**")
.permitAll().anyRequest().authenticated();
//Added by me, I've tried with and without this line with no difference to the result
//http.oauth2ResourceServer()
// .jwt()
// .jwtAuthenticationConverter(jwtAuthenticationConverter());
}
// Function added by me, from https://stackoverflow.com/a/63869532/18572146
private JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("myGroups");
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter);
return jwtAuthenticationConverter;
}
}
Code: Select all
security:
ignored: /favicon.ico, /lib/**, /js/**, /resources/**
basic:
enabled: false # Enable basic authentication.
headers:
cache: true # Enable cache control HTTP headers.
frame: true # Enable "X-Frame-Options" header.
xss: true # Enable cross site scripting (XSS) protection.
filter-order: 1 # Security filter chain order.
oauth2:
client:
clientId: ${APP_CLIENT_ID}
clientSecret: ${APP_CLIENT_SECRET}
accessTokenUri: ${APP_ACCESS_TOKEN_URI}
userAuthorizationUri: ${APP_USER_AUTH_URI}
scope: openid,profile
pre-established-redirect-uri: https://${APP_SERVER}:${APP_PORT}/login
registered-redirect-uri: https://${APP_SERVER}:${APP_PORT}/login
use-current-uri: false
resource:
userInfoUri: ${APP_USER_INFO_URI}
prefer-token-info: true
server:
port: ${APP_PORT}
use-forward-headers: true
tomcat:
remote-ip-header: x-forwarded-for
protocol-header: x-forwarded-proto
protocol-header-https-value: http
ssl:
enabled: true
#key-alias: tomcat
key-store: ${APP_KEY_STORE_FILE}
key-store-password: ${APP_KEY_STORE_PASSWORD}
key-store-type: PKCS12
error:
path: /error
spring:
resources:
chain:
enabled: true # Enable the Spring Resource Handling chain. Disabled by default unless at least one strategy has been enabled.
main:
banner-mode: "off" # Mode used to display the banner when the application runs.
allow-bean-definition-overriding: true # Turns off package conflict detector
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: ${APP_KEY_URI}
#Forward authorization token downstream
proxy:
auth:
routes:
customers: oauth2
stores: passthru
Ich vermute, dass es Probleme beim Dekodieren der Token gibt. Bemerkenswert ist, dass die einzige Änderung, die ich an application.yml für 2.4.13 vornehmen musste, das Hinzufügen von spring:security:oauth2:resourceserver:jwt:jwk-set-uri ist. Aber ich bin mir nicht sicher, wie ich das bestätigen oder beheben kann.