Java tutorial
/* * Copyright 2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.lateralthoughts.commons.security; import com.lateralthoughts.commons.account.Account; import org.bson.types.ObjectId; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import static com.google.common.collect.Lists.newArrayList; public class SecurityUtils { public static GrantedAuthority ROLE_USER = new SimpleGrantedAuthority("ROLE_USER"); public static GrantedAuthority ROLE_ADMIN = new SimpleGrantedAuthority("ROLE_ADMIN"); /** * Programmatically signs in the user with the given the user ID. */ static void signin(AccountWrapper userDetailsPrincipal) { SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken( userDetailsPrincipal, null, userDetailsPrincipal.getAuthorities())); } public static void signin(Account account, GrantedAuthority... roles) { final AccountWrapper userDetailsPrincipal = new AccountWrapper(account); userDetailsPrincipal.getAuthorities().addAll(newArrayList(roles)); signin(userDetailsPrincipal); } public static ObjectId connectedAccountId() { if (!isConnected()) { return null; } return connectedAccount().getId(); } public static Account connectedAccount() { return ((AccountWrapper) currentAuthentication().getPrincipal()).getAccount(); } public static Authentication currentAuthentication() { return SecurityContextHolder.getContext().getAuthentication(); } public static boolean isConnected() { return currentAuthentication() != null && currentAuthentication().getAuthorities().contains(ROLE_USER); } public static boolean isAdmin() { return currentAuthentication() != null && currentAuthentication().getAuthorities().contains(ROLE_ADMIN); } }