Java tutorial
/* * Copyright (c) 2015 Daniel Jabry * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.github.djabry.platform.service.security; import com.github.djabry.platform.domain.api.Group; import com.github.djabry.platform.domain.api.Permission; import com.github.djabry.platform.domain.api.UserAccount; import com.github.djabry.platform.service.api.PermissionMapper; import com.google.common.base.Function; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import lombok.Data; import lombok.RequiredArgsConstructor; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import javax.annotation.Nullable; import javax.validation.constraints.NotNull; import java.util.Collection; import java.util.Iterator; import java.util.Set; /** * Created by djabry on 05/01/15. */ @RequiredArgsConstructor @Data public class DefaultUserDetails implements UserDetails { static final Function<Permission, GrantedAuthority> PERMISSION_CONVERTER = new Function<Permission, GrantedAuthority>() { @Nullable @Override public GrantedAuthority apply(Permission permission) { return new SimpleGrantedAuthority(permission.permissionName); } }; @NotNull private final UserAccount userAccount; @NotNull private final PermissionMapper permissionMapper; /** * Returns the authorities granted to the user. Cannot return <code>null</code>. * * @return the authorities, sorted by natural key (never <code>null</code>) */ @Override public Collection<? extends GrantedAuthority> getAuthorities() { Set<Permission> permissions = permissionMapper.mapPermissions(userAccount.getRole()); Set<GrantedAuthority> authorities = Sets .newLinkedHashSet(Iterables.transform(permissions, PERMISSION_CONVERTER)); Iterator<Group> iterator = userAccount.getGroups().iterator(); while (iterator.hasNext()) { Group next = iterator.next(); Iterables.addAll(authorities, Iterables.transform(next.getPermissions(), PERMISSION_CONVERTER)); } return authorities; } /** * Returns the password used to authenticate the user. * * @return the password */ @Override public String getPassword() { return this.userAccount.getEncryptedPassword(); } /** * Returns the username used to authenticate the user. Cannot return <code>null</code>. * * @return the username (never <code>null</code>) */ @Override public String getUsername() { return this.userAccount.getUser().getUsername(); } /** * Indicates whether the user's account has expired. An expired account cannot be authenticated. * * @return <code>true</code> if the user's account is valid (ie non-expired), <code>false</code> if no longer valid * (ie expired) */ @Override public boolean isAccountNonExpired() { return userAccount.isAccountNonExpired(); } /** * Indicates whether the user is locked or unlocked. A locked user cannot be authenticated. * * @return <code>true</code> if the user is not locked, <code>false</code> otherwise */ @Override public boolean isAccountNonLocked() { return userAccount.isAccountNonLocked(); } /** * Indicates whether the user's credentials (password) has expired. Expired credentials prevent * authentication. * * @return <code>true</code> if the user's credentials are valid (ie non-expired), <code>false</code> if no longer * valid (ie expired) */ @Override public boolean isCredentialsNonExpired() { return userAccount.isCredentialsNonExpired(); } /** * Indicates whether the user is enabled or disabled. A disabled user cannot be authenticated. * * @return <code>true</code> if the user is enabled, <code>false</code> otherwise */ @Override public boolean isEnabled() { return userAccount.isEnabled(); } }