move createAccountFromPrincipal to service
This commit is contained in:
@ -1,32 +1,22 @@
|
||||
package mops.gruppen2.controllers;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import mops.gruppen2.security.Account;
|
||||
import org.keycloak.KeycloakPrincipal;
|
||||
import mops.gruppen2.services.KeyCloakService;
|
||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.context.annotation.SessionScope;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
|
||||
@SessionScope
|
||||
@Controller
|
||||
public class Gruppen2Controller {
|
||||
/**
|
||||
* Creates an Account.
|
||||
*
|
||||
* @param token Ein toller token
|
||||
* @return Account with current userdata
|
||||
*/
|
||||
private Account createAccountFromPrincipal(KeycloakAuthenticationToken token) {
|
||||
KeycloakPrincipal principal = (KeycloakPrincipal) token.getPrincipal();
|
||||
return new Account(
|
||||
principal.getName(),
|
||||
principal.getKeycloakSecurityContext().getIdToken().getEmail(),
|
||||
null,
|
||||
principal.getKeycloakSecurityContext().getIdToken().getGivenName(),
|
||||
principal.getKeycloakSecurityContext().getIdToken().getFamilyName(),
|
||||
token.getAccount().getRoles());
|
||||
|
||||
private final KeyCloakService keyCloakService;
|
||||
|
||||
public Gruppen2Controller(KeyCloakService keyCloakService) {
|
||||
this.keyCloakService = keyCloakService;
|
||||
}
|
||||
|
||||
/**Zeigt die index.html an.
|
||||
@ -38,7 +28,7 @@ public class Gruppen2Controller {
|
||||
@GetMapping("/")
|
||||
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
|
||||
public String index(KeycloakAuthenticationToken token, Model model) {
|
||||
model.addAttribute("account", createAccountFromPrincipal(token));
|
||||
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
|
||||
27
src/main/java/mops/gruppen2/services/KeyCloakService.java
Normal file
27
src/main/java/mops/gruppen2/services/KeyCloakService.java
Normal file
@ -0,0 +1,27 @@
|
||||
package mops.gruppen2.services;
|
||||
|
||||
import mops.gruppen2.security.Account;
|
||||
import org.keycloak.KeycloakPrincipal;
|
||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class KeyCloakService {
|
||||
|
||||
/**
|
||||
* Creates an Account.
|
||||
*
|
||||
* @param token Ein toller token
|
||||
* @return Account with current userdata
|
||||
*/
|
||||
public Account createAccountFromPrincipal(KeycloakAuthenticationToken token) {
|
||||
KeycloakPrincipal principal = (KeycloakPrincipal) token.getPrincipal();
|
||||
return new Account(
|
||||
principal.getName(),
|
||||
principal.getKeycloakSecurityContext().getIdToken().getEmail(),
|
||||
null,
|
||||
principal.getKeycloakSecurityContext().getIdToken().getGivenName(),
|
||||
principal.getKeycloakSecurityContext().getIdToken().getFamilyName(),
|
||||
token.getAccount().getRoles());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user