2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.security
;
18 import org
.springframework
.security
.Authentication
;
19 import org
.springframework
.security
.GrantedAuthority
;
20 import org
.springframework
.security
.context
.SecurityContext
;
21 import org
.springframework
.security
.context
.SecurityContextHolder
;
22 import org
.springframework
.security
.providers
.anonymous
.AnonymousAuthenticationToken
;
24 /** Static utilities */
25 public class SecurityUtils
{
27 private SecurityUtils() {
30 /** Whether the current thread has the admin role */
31 public static boolean hasCurrentThreadAuthority(String authority
) {
32 SecurityContext securityContext
= SecurityContextHolder
.getContext();
33 if (securityContext
!= null) {
34 Authentication authentication
= securityContext
.getAuthentication();
35 if (authentication
!= null) {
36 for (GrantedAuthority ga
: authentication
.getAuthorities())
37 if (ga
.getAuthority().equals(authority
))
45 * @return the authenticated username or null if not authenticated /
48 public static String
getCurrentThreadUsername() {
49 SecurityContext securityContext
= SecurityContextHolder
.getContext();
50 if (securityContext
!= null) {
51 Authentication authentication
= securityContext
.getAuthentication();
52 if (authentication
!= null) {
53 if (authentication
instanceof AnonymousAuthenticationToken
) {
56 return authentication
.getName();