public void authenticator() { // tag::authenticator[] AccountProvider accountProvider = getAccountProvider(); // build or obtain the AccountProvider to use Authenticator<AccountCredentialsToken> authenticator = Account.authenticator(accountProvider); // <1> // end::authenticator[] }
public void authenticator2() { // tag::authenticator2[] Authenticator<AccountCredentialsToken> authenticator = Account.authenticator(getAccountProvider(), new MyCredentialsMatcher() // <1> ); // end::authenticator2[] }
@Bean public AuthenticationManager authenticationManager(AuthenticationManagerBuilder auth, AccountProvider accountProvider) throws Exception { // <2> return auth.authenticationProvider( // <3> SpringSecurity.authenticationProvider(Account.authenticator(accountProvider), // <4> UsernamePasswordAuthenticationToken.class, // <5> upt -> AccountCredentialsToken.create(upt.getPrincipal().toString(), upt.getCredentials().toString()) // <6> )).build(); }
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[] }
@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()); }
@Override public Realm getContext(Class<?> type) { return Realm.builder() // .withResolver(AuthenticationToken.httpBasicResolver()) // <1> .withAuthenticator(Account.authenticator(getAccountProvider())) // <2> .withDefaultAuthorizer() // <3> .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()); }
public void contextResourceRealm() { // tag::realm2[] Context.get().classLoaderScope() // <1> .map(s -> s.put(Realm.CONTEXT_KEY, Realm.builder().withResolver(AuthenticationToken.httpBasicResolver()) .withAuthenticator(Account.authenticator(getAccountProvider())).withDefaultAuthorizer() .build())); // end::realm2[] }
public void basicresolver() { // tag::basicresolver[] AccountProvider accountProvider = getAccountProvider(); Realm realm = Realm.builder().withResolver(AuthenticationToken.httpBasicResolver()) // <1> .withAuthenticator(Account.authenticator(accountProvider)) // <2> .build(); // obtain the HttpRequest message, for example using a servlet request HttpRequest request = getHttpRequest(); try { Authentication authc = realm.authenticate(request); // <3> } catch (AuthenticationException e) { // handle authentication failures } // end::basicresolver[] }
public void auth() { // tag::auth[] 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)) // <2> .withDefaultAuthorizer().build(); try { Authentication authc = realm.authenticate(AuthenticationToken.accountCredentials("test", "pwd")); // <3> } catch (AuthenticationException e) { // handle authentication failures } // end::auth[] }
public void configureJaxrsApplication() { AccountProvider provider = id -> { // <5> // a test provider wich always returns an Account with given id and s3cr3t as password return Optional.ofNullable(Account.builder(id).credentials(Credentials.builder().secret("s3cr3t").build()) .enabled(true).build()); }; Realm realm = Realm.builder() // <6> .withResolver(AuthenticationToken.httpBasicResolver()) // <7> .withAuthenticator(Account.authenticator(provider)) // <8> .withDefaultAuthorizer().build(); ContextResolver<Realm> realmContextResolver = new ContextResolver<Realm>() { // <9> @Override public Realm getContext(Class<?> type) { return realm; } }; register(realmContextResolver); // <10> } // end::auth[]
@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[] }