@Override public Authentication authenticate(Message<?, ?> message, String... schemes) throws AuthenticationException { return getAuthContext().authenticate(message, schemes); }
@Override public Authentication authenticate(AuthenticationToken authenticationToken) throws AuthenticationException { return getAuthContext().authenticate(authenticationToken); }
public void authContext2() { // tag::authcontext2[] final Realm realm = Realm.builder().withDefaultAuthorizer().withAuthenticator(Account.authenticator(id -> { // <1> if ("usr".equals(id)) { return Optional.of(Account.builder(id).credentials(Credentials.builder().secret("pwd").build()) .withPermission("role1").build()); } return Optional.empty(); })).build(); AuthContext authContext = SpringSecurity.authContext(realm); // <2> authContext.authenticate(Account.accountCredentialsToken("usr", "pwd")); // <3> org.springframework.security.core.Authentication authc = SecurityContextHolder.getContext().getAuthentication(); // <4> String name = authc.getName(); // <5> Collection<? extends GrantedAuthority> authorities = authc.getAuthorities(); // <6> // end::authcontext2[] }
@Override public void filter(ContainerRequestContext requestContext) throws IOException { // check SecurityContext type if (!AuthContext.class.isAssignableFrom(requestContext.getSecurityContext().getClass())) { throw new IOException("Invalid SecurityContext type: expecting an AuthContext but found [" + requestContext.getSecurityContext().getClass().getName() + "]"); } final AuthContext authContext = (AuthContext) requestContext.getSecurityContext(); // check authenticated if (!authContext.getAuthentication().isPresent()) { LOGGER.debug(() -> "Authenticate request using AuthContext"); // authenticate try { authContext.authenticate(new JaxrsContainerHttpRequest(requestContext), schemes); } catch (UnsupportedMessageException e) { LOGGER.debug(() -> "Authentication error: aborting request", e); requestContext.abortWith(ResponseUtils.buildAuthenticationErrorResponse(schemes, null, null, HttpStatus.UNAUTHORIZED.getCode(), null)); } catch (AuthenticationException e) { LOGGER.debug(() -> "Authentication error: aborting request", e); requestContext.abortWith(ResponseUtils.buildAuthenticationErrorResponse(e, null)); } } }
if (request != null) { try { authContext.authenticate(VaadinHttpRequest.create(request));
@SuppressWarnings("unused") public void authContext() { // tag::authctx[] AccountProvider provider = id -> Optional.of(Account.builder(id).enabled(true) .credentials(Credentials.builder().secret("pwd").base64Encoded().build()).withPermission("role1") .build()); // <1> Realm realm = Realm.builder().withAuthenticator(Account.authenticator(provider)).withDefaultAuthorizer() .build(); // <2> AuthContext context = AuthContext.create(realm); // <3> boolean notAlreadyAuthenticated = context.isAuthenticated(); // <4> context.authenticate(AuthenticationToken.accountCredentials("test", "pwd")); // <5> Authentication authc = context.requireAuthentication(); // <6> context.unauthenticate(); // <7> // end::authctx[] }