本文整理汇总了Java中org.springframework.security.core.authority.AuthorityUtils类的典型用法代码示例。如果您正苦于以下问题:Java AuthorityUtils类的具体用法?Java AuthorityUtils怎么用?Java AuthorityUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AuthorityUtils类属于org.springframework.security.core.authority包,在下文中一共展示了AuthorityUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAuthentication
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的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: requestHeaderAuthenticationFilter
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
private RequestHeaderAuthenticationFilter requestHeaderAuthenticationFilter() throws Exception {
RequestHeaderAuthenticationFilter f = new RequestHeaderAuthenticationFilter();
f.setPrincipalRequestHeader("X-Forwarded-User");
f.setCredentialsRequestHeader("X-Forwarded-Access-Token");
f.setAuthenticationManager(authenticationManager());
f.setAuthenticationDetailsSource(
(AuthenticationDetailsSource<HttpServletRequest, PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails>)
(request) ->new PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails(
request,
AuthorityUtils.createAuthorityList("ROLE_AUTHENTICATED")
)
);
f.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler());
f.setExceptionIfHeaderMissing(false);
return f;
}
示例3: mapUserEntityToUserDetails
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
private UserDetails mapUserEntityToUserDetails(UserEntity userEntity) {
List<GrantedAuthority> authorities = AuthorityUtils.NO_AUTHORITIES;
if (userEntity.getRoles() != null && userEntity.getRoles().size() > 0) {
authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(
userEntity.getRoles().stream().map(r -> r.getScope().name()+":"+r.getName()).collect(Collectors.joining(","))
);
}
io.gravitee.management.idp.api.authentication.UserDetails userDetails = new io.gravitee.management.idp.api.authentication.UserDetails(
userEntity.getUsername(), userEntity.getPassword(), authorities);
userDetails.setFirstname(userEntity.getFirstname());
userDetails.setLastname(userEntity.getLastname());
userDetails.setEmail(userEntity.getEmail());
userDetails.setSource(RepositoryIdentityProvider.PROVIDER_TYPE);
userDetails.setSourceId(userEntity.getUsername());
return userDetails;
}
开发者ID:gravitee-io,项目名称:gravitee-management-rest-api,代码行数:21,代码来源:RepositoryAuthenticationProvider.java
示例4: configure
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
ClientDetailsServiceBuilder<InMemoryClientDetailsServiceBuilder>.ClientBuilder builder = clients
.inMemory().withClient(this.details.getClientId());
builder.secret(this.details.getClientSecret())
.resourceIds(this.details.getResourceIds().toArray(new String[0]))
.authorizedGrantTypes(
this.details.getAuthorizedGrantTypes().toArray(new String[0]))
.authorities(
AuthorityUtils.authorityListToSet(this.details.getAuthorities())
.toArray(new String[0]))
.scopes(this.details.getScope().toArray(new String[0]));
if (this.details.getRegisteredRedirectUri() != null) {
builder.redirectUris(
this.details.getRegisteredRedirectUri().toArray(new String[0]));
}
}
示例5: configure
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
ClientDetailsServiceBuilder<InMemoryClientDetailsServiceBuilder>.ClientBuilder builder = clients
.inMemory().withClient(this.details.getClientId());
builder.secret(this.details.getClientSecret())
.resourceIds(this.details.getResourceIds().toArray(new String[0]))
.authorizedGrantTypes(
this.details.getAuthorizedGrantTypes().toArray(new String[0]))
.authorities(
AuthorityUtils.authorityListToSet(this.details.getAuthorities())
.toArray(new String[0]))
.scopes(this.details.getScope().toArray(new String[0]));
if (this.details.getAutoApproveScopes() != null) {
builder.autoApprove(
this.details.getAutoApproveScopes().toArray(new String[0]));
}
if (this.details.getAccessTokenValiditySeconds() != null) {
builder.accessTokenValiditySeconds(
this.details.getAccessTokenValiditySeconds());
}
if (this.details.getRefreshTokenValiditySeconds() != null) {
builder.refreshTokenValiditySeconds(
this.details.getRefreshTokenValiditySeconds());
}
if (this.details.getRegisteredRedirectUri() != null) {
builder.redirectUris(
this.details.getRegisteredRedirectUri().toArray(new String[0]));
}
}
开发者ID:spring-projects,项目名称:spring-security-oauth2-boot,代码行数:31,代码来源:OAuth2AuthorizationServerConfiguration.java
示例6: oauth2ClientDetails
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
@Bean
@ConfigurationProperties(prefix = "security.oauth2.client")
public BaseClientDetails oauth2ClientDetails() {
BaseClientDetails details = new BaseClientDetails();
if (this.client.getClientId() == null) {
this.client.setClientId(UUID.randomUUID().toString());
}
details.setClientId(this.client.getClientId());
details.setClientSecret(this.client.getClientSecret());
details.setAuthorizedGrantTypes(Arrays.asList("authorization_code",
"password", "client_credentials", "implicit", "refresh_token"));
details.setAuthorities(
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
details.setRegisteredRedirectUri(Collections.<String>emptySet());
return details;
}
开发者ID:spring-projects,项目名称:spring-security-oauth2-boot,代码行数:17,代码来源:OAuth2AuthorizationServerConfiguration.java
示例7: testAuthorizationServerOverride
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
@Test
public void testAuthorizationServerOverride() {
this.context = new AnnotationConfigServletWebServerApplicationContext();
TestPropertyValues.of("security.oauth2.resourceId:resource-id")
.applyTo(this.context);
this.context.register(AuthorizationAndResourceServerConfiguration.class,
CustomAuthorizationServer.class, MinimalSecureWebApplication.class);
this.context.refresh();
BaseClientDetails config = new BaseClientDetails();
config.setClientId("client");
config.setClientSecret("secret");
config.setResourceIds(Arrays.asList("resource-id"));
config.setAuthorizedGrantTypes(Arrays.asList("password"));
config.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList("USER"));
config.setScope(Arrays.asList("read"));
assertThat(countBeans(AUTHORIZATION_SERVER_CONFIG)).isEqualTo(0);
assertThat(countBeans(RESOURCE_SERVER_CONFIG)).isEqualTo(1);
verifyAuthentication(config);
}
开发者ID:spring-projects,项目名称:spring-security-oauth2-boot,代码行数:20,代码来源:OAuth2AutoConfigurationTests.java
示例8: getAuthentication
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
public static Authentication getAuthentication(HttpServletRequest request) {
// 从Header中拿到token
String token = request.getHeader(HEADER_STRING);
if (token == null) {
token = getTokenFromCookis(request);
}
if (token != null && !token.isEmpty()) {
// 解析 Token
Claims claims = Jwts.parser().setSigningKey(SECRET)
.parseClaimsJws(token).getBody();
// 获取用户名
String user = claims.get("UserId").toString();
// 获取权限(角色)
List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("authorities"));
// 返回验证令牌
return user != null ? new UsernamePasswordAuthenticationToken(user, null, authorities) : null;
}
return null;
}
示例9: userDetailsService
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
@Bean
@Override
public UserDetailsService userDetailsService() {
return username -> {
UserAccount account = userAccountService.findByUsername(username);
if(account != null) {
RequestContext.getInstance().set(account);
return new User(account.getUserName(), account.getPassword(),
account.getEnabled(), account.getAccountNonExpired(),
true, account.getAccountNonLocked(),
AuthorityUtils.createAuthorityList(account.getAuthorities()));
} else {
throw new UsernameNotFoundException("could not find the user '"
+ username + "'");
}
};
}
示例10: runAs
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
/**
* Configures the Spring Security {@link SecurityContext} to be authenticated as the user with the given username and
* password as well as the given granted authorities.
*
* @param username must not be {@literal null} or empty.
* @param password must not be {@literal null} or empty.
* @param roles
*/
public static void runAs(String username, String password, String... roles) {
Assert.notNull(username, "Username must not be null!");
Assert.notNull(password, "Password must not be null!");
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(username, password, AuthorityUtils.createAuthorityList(roles)));
}
示例11: userDetailsService
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
@Bean
public UserDetailsService userDetailsService() {
return new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
// 通过用户名获取用户信息
Account account = accountRepository.findByName(name);
if (account != null) {
// 创建spring security安全用户
User user = new User(account.getName(), account.getPassword(),
AuthorityUtils.createAuthorityList(account.getRoles()));
return user;
} else {
throw new UsernameNotFoundException("用户[" + name + "]不存在");
}
}
};
}
示例12: getOauth2Request
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
/**
* Mock OAuth2Request
*
* @param withMockOAuth2Token
* @return
*/
private OAuth2Request getOauth2Request(WithMockOAuth2Token withMockOAuth2Token) {
String clientId = withMockOAuth2Token.clientId();
Map<String, String> requestParameters = Collections.emptyMap();
boolean approved = true;
String redirectUrl = withMockOAuth2Token.redirectUrl();
Set<String> responseTypes = Collections.emptySet();
Set<String> scopes = new HashSet<>(Arrays.asList(withMockOAuth2Token.scopes()));
Set<String> resourceIds = Collections.emptySet();
Map<String, Serializable> extensionProperties = Collections.emptyMap();
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(withMockOAuth2Token.authorities());
OAuth2Request oAuth2Request = new OAuth2Request(requestParameters, clientId, authorities, approved, scopes,
resourceIds, redirectUrl, responseTypes, extensionProperties);
return oAuth2Request;
}
开发者ID:anilallewar,项目名称:microservices-basics-spring-boot,代码行数:23,代码来源:WithOAuth2MockAccessTokenSecurityContextFactory.java
示例13: getAuthentication
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
/**
* Provide the mock user information to be used
*
* @param withMockOAuth2Token
* @return
*/
private Authentication getAuthentication(WithMockOAuth2Token withMockOAuth2Token) {
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(withMockOAuth2Token.authorities());
User userPrincipal = new User(withMockOAuth2Token.userName(), withMockOAuth2Token.password(), true, true, true,
true, authorities);
HashMap<String, String> details = new HashMap<String, String>();
details.put("user_name", withMockOAuth2Token.userName());
details.put("email", "[email protected]");
details.put("name", "Anil Allewar");
TestingAuthenticationToken token = new TestingAuthenticationToken(userPrincipal, null, authorities);
token.setAuthenticated(true);
token.setDetails(details);
return token;
}
开发者ID:anilallewar,项目名称:microservices-basics-spring-boot,代码行数:24,代码来源:WithOAuth2MockAccessTokenSecurityContextFactory.java
示例14: userDetailsService
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
@Bean
public UserDetailsService userDetailsService() {
return new UserDetailsService() {
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = dao.getUserByEmail(email);
if(user != null) {
return new org.springframework.security.core.userdetails.User(
user.getEmail(),
user.getPassword(),
user.valid(),
true,
true,
true,
AuthorityUtils.createAuthorityList(user.fetchAuthorities())
);
}
else {
throw new UsernameNotFoundException("Could not find that user");
}
}
};
}
示例15: setup
import org.springframework.security.core.authority.AuthorityUtils; //导入依赖的package包/类
@Before
public void setup() {
if (this.registry != null) {
this.registry.clear();
}
this.context = SecurityContextHolder.createEmptyContext();
this.context.setAuthentication(
new UsernamePasswordAuthenticationToken("username-" + UUID.randomUUID(),
"na", AuthorityUtils.createAuthorityList("ROLE_USER")));
this.changedContext = SecurityContextHolder.createEmptyContext();
this.changedContext.setAuthentication(new UsernamePasswordAuthenticationToken(
"changedContext-" + UUID.randomUUID(), "na",
AuthorityUtils.createAuthorityList("ROLE_USER")));
}