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

@ -3,6 +3,8 @@ package de.jottyfan.bico.modules;
import java.security.Principal;
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.Value;
import org.springframework.security.core.context.SecurityContextHolder;
@ -19,6 +21,8 @@ import de.jottyfan.bico.modules.profile.ProfileService;
*/
public abstract class CommonController {
private static final Logger LOGGER = LogManager.getLogger(CommonController.class);
@Autowired
private ProfileService profileService;
@ -28,10 +32,15 @@ public abstract class CommonController {
@ModelAttribute("hasBUrole")
public Boolean hasBURole(Principal principal) {
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) principal;
OAuth2User user = token.getPrincipal();
@SuppressWarnings("unchecked")
List<String> roles = (List<String>) user.getAttributes().get("roles");
return roles.contains("Bibelunterricht");
if (token != null) {
OAuth2User user = token.getPrincipal();
@SuppressWarnings("unchecked")
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}
# for development only
server.port = ${server.port}
# nextcloud open ID connection
spring.security.oauth2.client.provider.nextcloud.issuer-uri = ${nextcloud.issuer-uri}
spring.security.oauth2.client.registration.nextcloud.client-id = ${nextcloud.client-id}