本文整理汇总了Java中org.springframework.security.core.GrantedAuthority类的典型用法代码示例。如果您正苦于以下问题:Java GrantedAuthority类的具体用法?Java GrantedAuthority怎么用?Java GrantedAuthority使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GrantedAuthority类属于org.springframework.security.core包,在下文中一共展示了GrantedAuthority类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAuthentication
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
public Authentication getAuthentication(HttpServletRequest request) {
String token = request.getHeader(HEADER_STRING);
if (token != null) {
// parse the token.
String user = getUsername(token);
String roles = getBody(token).get("roles", String.class);
List<GrantedAuthority> grantedAuths =
AuthorityUtils.commaSeparatedStringToAuthorityList(roles);
return user != null ?
new UsernamePasswordAuthenticationToken(user, null,
grantedAuths) :
null;
}
return null;
}
示例2: loadUserByUsername
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
CalendarUser user = userRepository.findByEmail(username);
if (user == null)
throw new UsernameNotFoundException("username " + username
+ " not found");
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
for (Role role : user.getRoles()){
grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
}
return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), grantedAuthorities);
}
示例3: loadUserByUsername
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
if (StringUtils.isBlank(username)) {
throw new UsernameNotFoundException("用户名为空");
}
String password;
TUser tUser = iUserService.getByUsername(username);
if(tUser==null){
throw new UsernameNotFoundException("登录账号不存在");
}else{
password=tUser.getPassword();
}
Set<GrantedAuthority> authorities = new HashSet<>();
authorities.add(new SimpleGrantedAuthority("USER"));
return new org.springframework.security.core.userdetails.User(
username, password,
true,
true,
true,
true,
authorities);
}
示例4: authenticate
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
@Override
public Authentication authenticate(Authentication auth){
Usuario user = userRepository.findByEmail(auth.getName());
if (user == null) {
throw new BadCredentialsException("User not found");
}
String password = (String) auth.getCredentials();
if (!new BCryptPasswordEncoder().matches(password, user.getContraseña())) {
throw new BadCredentialsException("Wrong password");
}
List<GrantedAuthority> roles = new ArrayList<>();
for (String role : user.getRol()) {
roles.add(new SimpleGrantedAuthority(role));
}
return new UsernamePasswordAuthenticationToken(user.getEmail(), password, roles);
}
示例5: getAuthorities
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
public static List<String> getAuthorities() {
Authentication authentication = getAuthentication();
if (authentication == null) {
return Collections.EMPTY_LIST;
}
Collection<? extends GrantedAuthority> grantedAuthorityList = authentication
.getAuthorities();
List<String> authorities = new ArrayList<String>();
for (GrantedAuthority grantedAuthority : grantedAuthorityList) {
authorities.add(grantedAuthority.getAuthority());
}
return authorities;
}
示例6: authenticate
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
String email = token.getName();
CalendarUser user = email == null ? null : calendarService.findUserByEmail(email);
if(user == null) {
throw new UsernameNotFoundException("Invalid username/password");
}
// Database Password already encrypted:
String password = user.getPassword();
boolean passwordsMatch = passwordEncoder.matches(token.getCredentials().toString(), password);
if(!passwordsMatch) {
throw new BadCredentialsException("Invalid username/password");
}
Collection<? extends GrantedAuthority> authorities = CalendarUserAuthorityUtils.createAuthorities(user);
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(user, password, authorities);
return usernamePasswordAuthenticationToken;
}
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:21,代码来源:CalendarUserAuthenticationProvider.java
示例7: createToken
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
public String createToken(Authentication authentication, Boolean rememberMe) {
String authorities = authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.joining(","));
long now = (new Date()).getTime();
Date validity;
if (rememberMe) {
validity = new Date(now + this.tokenValidityInMillisecondsForRememberMe);
} else {
validity = new Date(now + this.tokenValidityInMilliseconds);
}
return Jwts.builder()
.setSubject(authentication.getName())
.claim(AUTHORITIES_KEY, authorities)
.signWith(SignatureAlgorithm.HS512, secretKey)
.setExpiration(validity)
.compact();
}
示例8: create
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
public static UserContext create(String tenantId, String orgId, List<GrantedAuthority> authorities) {
if (authorities == null || authorities.isEmpty()) {
throw new IllegalArgumentException("No authorities");
}
Optional<GrantedAuthority> maybeSuperAdmin
= authorities.stream()
.filter(authority -> authority.getAuthority().equals("ROLE_SUPER_ADMIN")).findAny();
if (maybeSuperAdmin.isPresent()) {
return new UserContext("*", "*", authorities);
}
Optional<GrantedAuthority> maybeTenantAdmin
= authorities.stream()
.filter(authority -> authority.getAuthority().equals("ROLE_TENANT_ADMIN")).findAny();
if (maybeTenantAdmin.isPresent()) {
return new UserContext(tenantId, "*", authorities);
}
return new UserContext(tenantId, orgId, authorities);
}
示例9: doGetGrantedResources
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
private List<Resource> doGetGrantedResources(List<? extends Resource> existedResources,
List<GrantedAuthority> roles) {
List<Resource> result = new ArrayList<>();
//always return the open resource
existedResources.stream().filter(res -> res.isOpen()).forEach(res -> result.add(res));
//and return the granted resource
existedResources.stream().filter(res -> !res.isOpen()).forEach(resource -> {
for (GrantedAuthority role : roles) {
ResourceRoleRelationship resourceRoleRelationship = resourceRoleRelationshipRepository.findByResourceCodeAndRoleCode(
resource.getCode(),
RbacUtils.buildRoleCode(role));
if (resourceRoleRelationship != null) {
result.add(resource);
break;
}
}
});
return result;
}
示例10: JwtUser
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
public JwtUser(
Long id,
String username,
String firstname,
String lastname,
String email,
String password, Collection<? extends GrantedAuthority> authorities,
boolean enabled,
Date lastPasswordResetDate
) {
this.id = id;
this.username = username;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.password = password;
this.authorities = authorities;
this.enabled = enabled;
this.lastPasswordResetDate = lastPasswordResetDate;
}
示例11: loadUserByUsername
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
@Override
@Transactional
public UserDetails loadUserByUsername(final String login) {
log.debug("Authenticating {}", login);
String lowercaseLogin = login.toLowerCase(Locale.ENGLISH);
Optional<User> userFromDatabase = userRepository.findOneWithAuthoritiesByLogin(lowercaseLogin);
return userFromDatabase.map(user -> {
if (!user.getActivated()) {
throw new UserNotActivatedException("User " + lowercaseLogin + " was not activated");
}
List<GrantedAuthority> grantedAuthorities = user.getAuthorities().stream()
.map(authority -> new SimpleGrantedAuthority(authority.getName()))
.collect(Collectors.toList());
return new org.springframework.security.core.userdetails.User(lowercaseLogin,
user.getPassword(),
grantedAuthorities);
}).orElseThrow(() -> new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the " +
"database"));
}
示例12: getGrantedAuthorities
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
@Override
public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {
Collection<? extends GrantedAuthority> authorities = delegate.getGrantedAuthorities(userData, username);
if (authorities != null) {
return authorities.stream()
.map(GrantedAuthority::getAuthority)
.map(a -> authorityToPermissionMap.get(a))
.filter(Objects::nonNull)
.filter(a -> !a.isEmpty())
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toSet());
} else {
return null;
}
}
示例13: enhance
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal();
String roles = "";
List<GrantedAuthority> grantedAuthorities = (List<GrantedAuthority>) customUserDetails.getAuthorities();
for (GrantedAuthority grantedAuthority : grantedAuthorities) {
roles = roles.concat(" " + grantedAuthority.getAuthority());
}
roles = roles.trim();
Map<String, Object> additionalInfo = new HashMap<>();
additionalInfo.put("uuid", customUserDetails.getId());
additionalInfo.put("role", roles);
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
}
示例14: isAuthorized
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
/**
* Check the authorization
*/
private boolean isAuthorized(final Collection<? extends GrantedAuthority> authorities, final String request, final HttpMethod method) {
final Map<String, Map<HttpMethod, List<Pattern>>> authorizationsCache = authorizationResource.getAuthorizations().get(
AuthorizationType.API);
// Check the authorization
if (authorizationsCache != null) {
for (final GrantedAuthority authority : authorities) {
final Map<HttpMethod, List<Pattern>> authorizations = authorizationsCache.get(authority.getAuthority());
if (authorizations != null && match(authorizations.get(method), request)) {
// Granted access
return true;
}
}
}
// No authorization found
return false;
}
示例15: getCurrentRoles
import org.springframework.security.core.GrantedAuthority; //导入依赖的package包/类
public Set<String> getCurrentRoles() {
Set<String> roles = new HashSet<String>();
try {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth!=null && auth.isAuthenticated()) {
Object principal = auth.getPrincipal();
if (principal instanceof UserDetails) {
for (GrantedAuthority ga : ((UserDetails)principal).getAuthorities()) {
roles.add(ga.getAuthority());
}
}
}
} catch (Exception e) {
log.error("Can't get roles", e);
}
return roles;
}