Der OAuth2-Prinzipal ist nach der Authentifizierung in Spring Boot leerJava

Java-Forum
Guest
 Der OAuth2-Prinzipal ist nach der Authentifizierung in Spring Boot leer

Post by Guest »

Ich habe ein altes Spring Boot-Projekt geerbt. Im Projekt melden wir uns über einen Unternehmens-OIDC-Server an. Spring Boot stellt sicher, dass wir authentifiziert sind, und verfügt dann über einige Endpunkte, die wir erreichen können. Ich versuche, eine Autorisierung hinzuzufügen. Wir überprüfen das JWT-Token und stellen die Reaktionsseite nur bereit, wenn sie Mitglied einer bestimmten Gruppe sind. Dabei habe ich Spring Boot von 2.0.7 auf 2.4.13 aktualisiert (immer noch alt, ich weiß, aber ich kenne diesen Code nicht und versuche, Änderungen zu minimieren).
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;
}
}
application.yml

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
In 2.0.7 konnte ich mich anmelden, eine JSESSIONID erhalten und wenn ich den Endpunkt /userinfo erreichte, wurde ein ausgefüllter Principal erfolgreich protokolliert. In 2.4.13 kann ich mich anmelden und eine JSESSIONID erhalten, aber wenn ich den Endpunkt /userinfo erreiche, erhalte ich eine Nullzeigerausnahme. Wenn ich stattdessen versuche, einen Endpunkt zu verwenden, der direkt auf den Principal zugreift, funktioniert er wieder in 2.0.7, ist aber in 2.4.13 nicht aufgefüllt (der Name ist „unbekannt“ und selbst wenn ich den Aufruf von jwtAuthenticationConverter auskommentiere, ist die einzige Rolle die Standardeinstellung „ ROLE_USER").
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.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post