@Override protected AuthContext getAuthContext(Realm realm) { return AuthContext.create(realm); }
/** * Create an {@link AuthContext} which uses the Spring Security {@link SecurityContext} as authentication holder. * The default {@link SecurityContextHolder#getContext()} method is used to obtain the Spring Security * {@link SecurityContext} reference. * @param realm The {@link Realm} to which the auth context is bound (not null) * @return A new {@link AuthContext} bound to given realm and using the Spring Security {@link SecurityContext} as * authentication holder */ static AuthContext authContext(Realm realm) { return AuthContext.create(realm, new SpringSecurityAuthenticationHolder()); }
public void customAuthenticationHolder() { AuthContext.create(getRealm(), new ThreadLocalAuthenticationHolder()); // <2> } // end::holder[]
@SuppressWarnings("unused") public void context() { // tag::context[] final AuthContext authContext = AuthContext.create(getRealm()); Context.get().classLoaderScope().map(scope -> scope.put(AuthContext.CONTEXT_KEY, authContext)); // <1> Optional<AuthContext> currentAuthContext = AuthContext.getCurrent(); // <2> AuthContext requiredAuthContext = AuthContext.require(); // <3> // end::context[] }
@Bean // <3> @VaadinSessionScope public AuthContext authContext() { AccountProvider ap = id -> { // Only a user with username 'username1' is available if ("username1".equals(id)) { // setup the user password and assign the role 'role1' return Optional.of(Account.builder(id).credentials(Credentials.builder().secret("s3cr3t").build()) .withPermission("role1").build()); } return Optional.empty(); }; return AuthContext.create(Realm.builder() // authenticator using the AccountProvider .withAuthenticator(Account.authenticator(ap)) // default authorizer .withDefaultAuthorizer().build()); }
@Bean // <3> @VaadinSessionScope public AuthContext authContext() { AccountProvider ap = id -> { // Only a user with username 'username1' is available if ("username1".equals(id)) { // setup the user password and assign the role 'role1' return Optional.of(Account.builder(id).credentials(Credentials.builder().secret("s3cr3t").build()) .withPermission("role1").build()); } return Optional.empty(); }; return AuthContext.create(Realm.builder() // authenticator using the AccountProvider .withAuthenticator(Account.authenticator(ap)) // default authorizer .withDefaultAuthorizer().build()); }
/** * Create an {@link AuthContext} which uses the Spring Security {@link SecurityContext} as authentication holder. * The default {@link SecurityContextHolder#getContext()} method is used to obtain the Spring Security * {@link SecurityContext} reference. * <p> * A default {@link Realm} is created using the default authorizer and registering an {@link Authenticator} hich * supports {@link SpringSecurityAuthenticationToken} types and uses the Spring Security * {@link AuthenticationManager} to perform the authentication operations. * </p> * @param authenticationManager The Spring Security {@link AuthenticationManager} (not null) * @param accountCredentialsAuthenticator Whether to register in Realm an Authenticator for the default * {@link AccountCredentialsToken} which uses the Spring Security {@link AuthenticationManager} to perform * the authentication operations. * @return A new {@link AuthContext} instance * @see #authContext(Realm) */ static AuthContext authContext(AuthenticationManager authenticationManager, boolean accountCredentialsAuthenticator) { final Realm.Builder realm = Realm.builder().withDefaultAuthorizer() .withAuthenticator(authenticator(authenticationManager)); if (accountCredentialsAuthenticator) { realm.withAuthenticator(accountCredentialsAuthenticator(authenticationManager)); } return AuthContext.create(realm.build(), new SpringSecurityAuthenticationHolder()); }
@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[] }