Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2c98eb7b

Přidáno uživatelem Jakub Danek před více než 5 roky(ů)

re #37 implemented AuthorizationService interface for non-static
access to Spring Security context.

Zobrazit rozdíly:

server/src/main/java/org/danekja/ymanager/business/AuthorizationService.java
1
package org.danekja.ymanager.business;
2

  
3
import org.danekja.ymanager.domain.User;
4

  
5
/**
6
 * Helper service which helps to get information regarding currently authenticated user and his permissions.
7
 */
8
public interface AuthorizationService {
9
    /**
10
     * @return currently signed-in user or null
11
     */
12
    User getCurrentUser();
13

  
14
    /**
15
     * @return true if there is a user associated with the current session
16
     */
17
    boolean isSignedIn();
18
}
server/src/main/java/org/danekja/ymanager/business/impl/SpringAuthorizationService.java
1
package org.danekja.ymanager.business.impl;
2

  
3
import org.danekja.ymanager.business.AuthorizationService;
4
import org.danekja.ymanager.domain.User;
5
import org.springframework.security.authentication.AnonymousAuthenticationToken;
6
import org.springframework.security.core.Authentication;
7
import org.springframework.security.core.context.SecurityContextHolder;
8
import org.springframework.stereotype.Service;
9

  
10
/**
11
 * {@link AuthorizationService} implementation based on Spring Security framework.
12
 */
13
@Service
14
public class SpringAuthorizationService implements AuthorizationService {
15

  
16
    @Override
17
    public User getCurrentUser() {
18
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
19
        return !(auth instanceof AnonymousAuthenticationToken) ? (User) auth.getPrincipal() : null;
20
    }
21

  
22
    @Override
23
    public boolean isSignedIn() {
24
        return getCurrentUser() != null;
25
    }
26

  
27
}

Také k dispozici: Unified diff