public void authenticator1() { // tag::authenticator1[] Realm realm = getRealm(); try { Authentication authc = realm.authenticate(new MyAuthenticationToken("test")); // <1> } catch (AuthenticationException e) { // handle failed authentication } // end::authenticator1[] }
public void authenticate() { Realm realm = Realm.builder().withAuthenticator(new MyAuthenticator()).build(); // <4> try { Authentication authc = realm.authenticate(new MyAuthenticationToken("test")); // <5> } catch (AuthenticationException e) { // handle failed authentication } } // end::authenticator[]
public void authenticator2() { // tag::authenticator2[] AuthenticationManager authenticationManager = getAuthenticationManager(); // <1> Realm realm = Realm.builder().withDefaultAuthorizer() .withAuthenticator(SpringSecurity.authenticator(authenticationManager)) // <2> .build(); Authentication authc = realm.authenticate(SpringSecurityAuthenticationToken.account("user", "pwd1")); // <3> // end::authenticator2[] }
public void tokenResolvers() { // tag::tokenresolver[] AuthenticationTokenResolver<MyMessage> myResolver = AuthenticationTokenResolver.create(MyMessage.class, // <1> msg -> msg.getHeader("MY_HEADER").map(value -> new MyMessageAuthenticationToken(value)) // <2> ); // end::tokenresolver[] // tag::realmmsg[] Realm realm = Realm.builder().withResolver(myResolver) // <1> .withAuthenticator(new MyAuthenticator()) // <2> .build(); MyMessage message = new MyMessage(); Authentication authc = realm.authenticate(message); // <3> // end::realmmsg[] }
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 authenticator() throws IOException { // tag::authenticator[] JwtConfiguration configuration = JwtConfiguration.builder() // <1> .issuer("MyIssuer") // JWT token issuer .expireTime(10000) // 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(JwtSignatureAlgorithm.HS256) // use HS256 as signature algorithm .sharedKey(new byte[] { 1, 2, 3 }) // shared key to use with the symmetric signing algorithm .build(); // JWT authenticator JwtAuthenticator jwtAuthenticator = JwtAuthenticator.builder().configuration(configuration) // <2> .issuer("allowedIssuer") // <3> .withRequiredClaim("myClaim") // <4> .build(); // Realm Realm realm = Realm.builder().withAuthenticator(jwtAuthenticator) // <5> .withDefaultAuthorizer().build(); Authentication authc = realm.authenticate(AuthenticationToken.bearer("TheJWTtokenHere...")); // <6> realm = Realm.builder().withAuthenticator(jwtAuthenticator) // .withResolver(AuthenticationToken.httpBearerResolver()) // <7> .withDefaultAuthorizer().build(); HttpRequest request = obtainHttpRequest(); authc = realm.authenticate(request); // <8> // end::authenticator[] }