2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.vaadin
.util
;
11 import java
.util
.HashMap
;
14 import org
.apache
.logging
.log4j
.LogManager
;
15 import org
.apache
.logging
.log4j
.Logger
;
16 import org
.springframework
.security
.core
.Authentication
;
17 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
19 import eu
.etaxonomy
.cdm
.common
.URI
;
25 public class CdmVaadinAuthentication
{
27 private static final Logger logger
= LogManager
.getLogger();
29 public static final String KEY
= "key_authentication";
31 Map
<String
, Authentication
> hostAuthenticationMap
= new HashMap
<>();
33 public void addAuthentication(URI requestSourceUri
, String requestSourceContext
, Authentication authentication
) {
34 addAuthentication(getRequestSource(requestSourceUri
, requestSourceContext
), authentication
);
37 public void addAuthentication(String requestSource
, Authentication authentication
) {
38 if(requestSource
== null || requestSource
.isEmpty()) {
39 throw new IllegalStateException("When setting authentication, host cannot be null or empty");
42 if(authentication
== null) {
43 throw new IllegalStateException("When setting authentication, authentication object cannot be null");
45 hostAuthenticationMap
.put(requestSource
, authentication
);
48 public boolean isAuthenticated(URI uri
, String context
) {
49 if(uri
!= null && context
!= null && !context
.isEmpty()) {
50 Authentication authentication
= hostAuthenticationMap
.get(getRequestSource(uri
, context
));
51 if(authentication
!= null) {
52 return authentication
.isAuthenticated();
58 public Authentication
getAuthentication(URI uri
, String context
){
59 return hostAuthenticationMap
.get(getRequestSource(uri
, context
));
62 public boolean setSecurityContextAuthentication(URI uri
, String context
) {
63 if(uri
!= null && context
!= null && !context
.isEmpty()) {
64 Authentication authentication
= hostAuthenticationMap
.get(getRequestSource(uri
, context
));
65 if(authentication
!= null && authentication
.isAuthenticated()) {
66 SecurityContextHolder
.getContext().setAuthentication(authentication
);
73 public static String
getRequestSource(URI uri
, String context
) {
74 String source
= uri
.getHost() + ":" + String
.valueOf(uri
.getPort()) + context
;
75 logger
.warn(" request source : " + source
);