当前位置: 首页>>代码示例>>Java>>正文


Java Authentication类代码示例

本文整理汇总了Java中com.holonplatform.auth.Authentication的典型用法代码示例。如果您正苦于以下问题:Java Authentication类的具体用法?Java Authentication怎么用?Java Authentication使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Authentication类属于com.holonplatform.auth包,在下文中一共展示了Authentication类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: issue

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
@GET
@Path("/issue")
@Produces(MediaType.TEXT_PLAIN)
public Response issue(@Context SecurityContext securityContext) {

	// get Authentication
	Authentication authc = (Authentication) securityContext.getUserPrincipal();

	// configuration
	JwtConfiguration configuration = ResourceUtils.lookupResource(getClass(), JwtConfiguration.class, providers)
			.orElseThrow(() -> new InternalServerErrorException("JWT configuration not available"));

	// build JWT
	String jwt = JwtTokenBuilder.buildJwtToken(configuration, authc, UUID.randomUUID().toString());
	// ok
	return Response.ok(jwt, MediaType.TEXT_PLAIN).build();

}
 
开发者ID:holon-platform,项目名称:holon-examples,代码行数:19,代码来源:JwtIssuerEndpoint.java

示例2: permissions

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
public void permissions() {
	// tag::permissions[]
	final Permission p1 = Permission.create("role1");
	final Permission p2 = Permission.create("role2");
	
	// build an Authentication and grant the two permissions to it
	Authentication authc = Authentication.builder("test").permission(p1).permission(p2).build();
	
	// Realm with default authorizer
	Realm realm = Realm.builder().withDefaultAuthorizer().build();
	
	// permission checking
	boolean permitted = realm.isPermitted(authc, p1);
	permitted = realm.isPermitted(authc, p1, p2);
	permitted = realm.isPermitted(authc, "p1");
	permitted = realm.isPermittedAny(authc, p1, p2);
	permitted = realm.isPermittedAny(authc, "p1", "p2");
	// end::permissions[]
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:20,代码来源:ExampleRealm.java

示例3: checkPermitted

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
@Override
protected boolean checkPermitted(Authentication authentication, Collection<? extends Permission> permissions,
		boolean all) {
	if (authentication != null && authentication.isRoot()) {
		return true;
	}
	if (authentication != null && permissions != null && !permissions.isEmpty()) {
		Collection<Permission> granted = authentication.getPermissions();
		if (granted != null && !granted.isEmpty()) {
			if (all) {
				return granted.containsAll(permissions);
			} else {
				for (Permission p : permissions) {
					if (granted.contains(p)) {
						return true;
					}
				}
			}
		}
	}
	return false;
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:23,代码来源:DefaultAuthorizer.java

示例4: equals

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
@Override
public boolean equals(Object obj) {
	if (this == obj) {
		return true;
	}
	if (obj == null) {
		return false;
	}
	if (!(obj instanceof Authentication)) {
		return false;
	}
	Authentication other = (Authentication) obj;
	if (name == null) {
		if (other.getName() != null)
			return false;
	} else if (!name.equals(other.getName()))
		return false;
	return true;
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:20,代码来源:DefaultAuthentication.java

示例5: authenticate

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Authentication authenticate(Message<?, ?> message, String... schemes) throws AuthenticationException {

	if (message == null) {
		throw new UnexpectedAuthenticationException("Null Message");
	}

	// get suitable resolvers
	List<AuthenticationTokenResolver<Message>> resolvers = getResolversForMessageType(
			(Class<Message>) message.getClass());
	if (resolvers.isEmpty()) {
		throw new UnsupportedMessageException(
				"No AuthenticationTokenResolver available for message type " + message.getClass().getName());
	}

	// perform authentication

	return authenticate(resolveAuthenticationToken(message, resolvers, schemes).orElseThrow(
			() -> new UnsupportedMessageException("No AuthenticationTokenResolver resolved message" + message)));
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:22,代码来源:DefaultRealm.java

示例6: info

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
@PermitAll
@GET
@Path("name")
@Produces(MediaType.TEXT_PLAIN)
public String info(@Context SecurityContext securityContext) {
	Authentication authc = (Authentication) securityContext.getUserPrincipal();
	return authc.getName();
}
 
开发者ID:holon-platform,项目名称:holon-jaxrs,代码行数:9,代码来源:TestAuthzStd.java

示例7: getAccountRoles

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
@PermitAll
@GET
@Path("/roles")
@Produces(MediaType.TEXT_PLAIN)
public String getAccountRoles(@Context SecurityContext securityContext) {
	// get Authentication
	Authentication authc = (Authentication) securityContext.getUserPrincipal();
	// get roles
	return authc.getPermissions().stream().map(p -> p.getPermission().orElse(null))
			.collect(Collectors.joining(","));
}
 
开发者ID:holon-platform,项目名称:holon-examples,代码行数:12,代码来源:ProtectedEndpoint.java

示例8: authContext

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
@SuppressWarnings("unused")
public void authContext() {
	// tag::authctx[]
	AccountProvider provider = id -> Optional.of(Account.builder(id).enabled(true)
			.credentials(Credentials.builder().secret("pwd").base64Encoded().build()).permission("role1").build()); // <1>

	Realm realm = Realm.builder().authenticator(Account.authenticator(provider)).withDefaultAuthorizer().build(); // <2>

	AuthContext context = AuthContext.create(realm); // <3>

	context.addAuthenticationListener(
			a -> System.out.println((a != null) ? "Authenticated: " + a.getName() : "Unauthenticated")); // <4>

	try {
		
		context.authenticate(AuthenticationToken.accountCredentials("test", "pwd")); // <5>

		Authentication authc = context.getAuthentication()
				.orElseThrow(() -> new IllegalStateException("Context not authenticated")); // <6>

		boolean permitted = context.isPermitted("role1"); // <7>

		context.unauthenticate(); // <8>
		
	} catch (AuthenticationException e) {
		// handle authentication failures
	}
	// end::authctx[]
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:30,代码来源:ExampleAuthContext.java

示例9: authenticate

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
@Override
public Authentication authenticate(MyAuthenticationToken authenticationToken) throws AuthenticationException {
	if (!"test".equals(authenticationToken.getPrincipal())) { // <4>
		throw new UnknownAccountException();
	}
	return Authentication.builder(authenticationToken.principalName).build();
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:8,代码来源:ExampleRealm.java

示例10: resolver

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
public void resolver() {
	// tag::resolver[]
	Realm realm = Realm.builder().resolver(AuthenticationToken.httpBasicResolver())
			.authenticator(new MyAuthenticator()).build(); // <1>

	HttpRequest request = null; // obtain the HttpRequest message in real world code
	try {
		Authentication authc = realm.authenticate(request); // <2>
	} catch (AuthenticationException e) {
		// handle failed authentication
	}
	// end::resolver[]
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:14,代码来源:ExampleRealm.java

示例11: build

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
public void build() throws IOException {
	// tag::build[]
	JwtConfiguration configuration = JwtConfiguration
			.build(JwtConfigProperties.builder().withPropertySource("jwt.properties").build()); // <1>

	Authentication authc = Authentication.builder("test").permission("role1").parameter("name", "TestName").build(); // <2>

	String jwtToken = JwtTokenBuilder.buildJwtToken(configuration, authc, UUID.randomUUID().toString()); // <3>
	// end::build[]
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:11,代码来源:ExampleJwt.java

示例12: authenticator

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
public void authenticator() throws IOException {
	// tag::authenticator[]
	JwtConfiguration configuration = JwtConfiguration.builder().issuer("MyIssuer") // set the JWT token issuer
			.expireTime(10000) // token expire time in milliseconds
			.includeDetails(true) // include the Authentication details in JWT token generation
			.includePermissions(true) // include the Authentication permissions in JWT token generation
			.signatureAlgorithm("HS256") // use HS256 as signature algorithm
			.sharedKey(new byte[] { 1, 2, 3 }) // shared key to use with the symmetric signing algorithm
			.build();

	// Build the Jwt authenticator using the JwtConfiguration
	JwtAuthenticator jwtAuthenticator = JwtAuthenticator.builder().configuration(configuration)
			.issuer("allowedIssuer").requiredClaim("myClaim").build();

	// Build a Realm and register the authenticator
	Realm realm = Realm.builder().authenticator(jwtAuthenticator).withDefaultAuthorizer().build();

	try {

		// Authentication using a bearer token
		Authentication authc = realm.authenticate(AuthenticationToken.bearer("TheJWTtokenHere..."));

		// Authentication using a message
		HttpRequest request = null; // expected an HttpRequest message with an 'Authorization' header with 'Bearer:
									// JWTtokenValue'
		authc = realm.authenticate(request);

	} catch (AuthenticationException e) {
		// handle authentication failures
	}
	// end::authenticator[]
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:33,代码来源:ExampleJwt.java

示例13: auth

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
public void auth() {
	// tag::auth[]
	AccountProvider provider = id -> Optional.of(Account.builder(id).enabled(true)
			.credentials(Credentials.builder().secret("pwd").base64Encoded().build()).permission("role1").build()); // <1>

	Realm realm = Realm.builder().authenticator(Account.authenticator(provider)).withDefaultAuthorizer().build(); // <2>

	try {
		Authentication authc = realm.authenticate(AuthenticationToken.accountCredentials("test", "pwd")); // <3>
	} catch (AuthenticationException e) {
		// handle authentication failures
	}
	// end::auth[]
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:15,代码来源:ExampleAccount.java

示例14: buildJWT

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
private static String buildJWT(Authentication authentication, String id, String issuer, Long timeToLiveMs,
		SignatureAlgorithm algorithm, Key privateKey, byte[] signingKey, AuthPart... includeParts) {

	if (authentication == null) {
		throw new IllegalArgumentException("Null Authentication");
	}

	JwtBuilder builder = createJWT(id, authentication.getName(), issuer,
			(timeToLiveMs != null) ? timeToLiveMs.longValue() : -1);

	// sign

	if (privateKey != null || signingKey != null) {
		if (algorithm == null) {
			throw new IllegalArgumentException("Null signature algorithm");
		}
		if (privateKey != null) {
			builder.signWith(algorithm, privateKey);
		} else {
			builder.signWith(algorithm, signingKey);
		}
	}

	// auth parts

	if (includeParts != null) {
		for (AuthPart part : includeParts) {
			processAuthPart(builder, authentication, part);
		}
	}

	return builder.compact();
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:34,代码来源:JwtTokenBuilder.java

示例15: processAuthPart

import com.holonplatform.auth.Authentication; //导入依赖的package包/类
private static void processAuthPart(JwtBuilder builder, Authentication authentication, AuthPart part) {
	if (part != null) {
		switch (part) {
		case PERMISSIONS:
			processAuthPermissions(builder, authentication);
			break;
		case DETAILS:
			processAuthDetails(builder, authentication);
			break;
		default:
			break;
		}
	}
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:15,代码来源:JwtTokenBuilder.java


注:本文中的com.holonplatform.auth.Authentication类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。