Java tutorial
/* * ============================================================================= * * Copyright (c) 2013, Marco Molteni ("http://javaee.ch") * * 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 ch.javaee.basicMvc.service; import ch.javaee.basicMvc.domain.User; import ch.javaee.basicMvc.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.ArrayList; import java.util.Collection; import java.util.List; @Transactional(readOnly = true) @Component("myUserDetailsService") public class MyUserDetailsService implements UserDetailsService { @Autowired private UserRepository userRepository; @PersistenceContext private EntityManager entityManager; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { try { User domainUser = (User) entityManager.createQuery("from user u where u.email = :email") .setParameter("email", username).getSingleResult(); boolean enabled = true; boolean accountNonExpired = true; boolean credentialsNonExpired = true; boolean accountNonLocked = true; return new org.springframework.security.core.userdetails.User(domainUser.getEmail(), domainUser.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(domainUser.getRole().getRole())); } catch (Exception e) { throw new RuntimeException(e); } } /** * Retrieves a collection of {@link GrantedAuthority} based on a numerical role * * @param role the numerical role * @return a collection of {@link GrantedAuthority */ public Collection<? extends GrantedAuthority> getAuthorities(Integer role) { List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role)); return authList; } /** * Converts a numerical role to an equivalent list of roles * * @param role the numerical role * @return list of roles as as a list of {@link String} */ public List<String> getRoles(Integer role) { List<String> roles = new ArrayList<String>(); if (role.intValue() == 1) { roles.add("ROLE_USER"); roles.add("ROLE_ADMIN"); } else if (role.intValue() == 2) { roles.add("ROLE_USER"); } return roles; } /** * Wraps {@link String} roles to {@link SimpleGrantedAuthority} objects * * @param roles {@link String} of roles * @return list of granted authorities */ public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) { List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); for (String role : roles) { authorities.add(new SimpleGrantedAuthority(role)); } return authorities; } }