fixed null on token

This commit is contained in:
Jottyfan
2023-12-26 21:01:06 +01:00
parent 47bcd311ea
commit f1f9a5be9b
3 changed files with 14 additions and 8 deletions

View File

@ -8,7 +8,7 @@ plugins {
} }
group = 'de.jottyfan.bico' group = 'de.jottyfan.bico'
version = '0.0.6' version = '0.0.7'
description = """BibleClassOrganizer""" description = """BibleClassOrganizer"""

View File

@ -3,6 +3,8 @@ package de.jottyfan.bico.modules;
import java.security.Principal; import java.security.Principal;
import java.util.List; import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
@ -19,6 +21,8 @@ import de.jottyfan.bico.modules.profile.ProfileService;
*/ */
public abstract class CommonController { public abstract class CommonController {
private static final Logger LOGGER = LogManager.getLogger(CommonController.class);
@Autowired @Autowired
private ProfileService profileService; private ProfileService profileService;
@ -28,10 +32,15 @@ public abstract class CommonController {
@ModelAttribute("hasBUrole") @ModelAttribute("hasBUrole")
public Boolean hasBURole(Principal principal) { public Boolean hasBURole(Principal principal) {
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) principal; OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) principal;
OAuth2User user = token.getPrincipal(); if (token != null) {
@SuppressWarnings("unchecked") OAuth2User user = token.getPrincipal();
List<String> roles = (List<String>) user.getAttributes().get("roles"); @SuppressWarnings("unchecked")
return roles.contains("Bibelunterricht"); List<String> roles = (List<String>) user.getAttributes().get("roles");
return roles.contains("Bibelunterricht");
} else {
LOGGER.warn("token is null, no roles can be detected");
return false;
}
} }
/** /**

View File

@ -9,9 +9,6 @@ spring.datasource.password = ${db.password}
server.servlet.context-path = ${my.context-path:/BiCO} server.servlet.context-path = ${my.context-path:/BiCO}
# for development only
server.port = ${server.port}
# nextcloud open ID connection # nextcloud open ID connection
spring.security.oauth2.client.provider.nextcloud.issuer-uri = ${nextcloud.issuer-uri} spring.security.oauth2.client.provider.nextcloud.issuer-uri = ${nextcloud.issuer-uri}
spring.security.oauth2.client.registration.nextcloud.client-id = ${nextcloud.client-id} spring.security.oauth2.client.registration.nextcloud.client-id = ${nextcloud.client-id}