@Override public Authentication authenticate(MyAuthenticationToken authenticationToken) throws AuthenticationException { if (!"test".equals(authenticationToken.getPrincipal())) { // <3> throw new UnknownAccountException(); } return Authentication.builder(authenticationToken.principalName).build(); }
public void build1() throws IOException { // tag::build1[] JwtConfiguration configuration = JwtConfiguration .build(JwtConfigProperties.builder().withPropertySource("jwt.properties").build()); // <1> Authentication authc = Authentication.builder("test").build(); // <2> String jwt = JwtTokenBuilder.get().buildJwt(configuration, authc); // <3> jwt = JwtTokenBuilder.get().buildJwt(configuration, authc, UUID.randomUUID().toString()); // <4> // end::build1[] }
return auth.build();
public void authenticator3() { // tag::authenticator3[] Authenticator<MyAuthenticationToken> authenticator = Authenticator.create(MyAuthenticationToken.class, // <1> token -> { // check authentication token information token.getPrincipal(); token.getCredentials(); boolean valid = true; // ... // if not valid, throw an exception if (!valid) { throw new InvalidCredentialsException(); } // otherwise, return the authenticated principal representation return Authentication.builder("thePrincipalName").build(); }); try { Authentication authc = authenticator.authenticate(new MyAuthenticationToken("test")); // <2> } catch (AuthenticationException e) { // <3> } // end::authenticator3[] }
public void permissions() { // tag::permissions[] final Permission p1 = Permission.create("role1"); // <1> final Permission p2 = Permission.create("role2"); // <2> Authentication authc = Authentication.builder("test").withPermission(p1).withPermission(p2).build(); // <3> // Realm with default authorizer Realm realm = Realm.builder().withDefaultAuthorizer().build(); // <4> // permission checking boolean permitted = realm.isPermitted(authc, p1); // <5> permitted = realm.isPermitted(authc, "role1"); // <6> permitted = realm.isPermittedAny(authc, p1, p2); // <7> permitted = realm.isPermittedAny(authc, "role1", "role2"); // <8> boolean notPermitted = realm.isPermitted(authc, "other_role"); // <9> // end::permissions[] }
public void parse() throws IOException { // tag::parse[] JwtConfiguration configuration = JwtConfiguration.builder().includeDetails(true).includePermissions(true) .build(); // <1> String jwt = getJwt(); Authentication authc = JwtTokenParser.get().parseJwt(configuration, jwt).build(); // <2> // end::parse[] }
public void build3() throws IOException { // tag::build3[] JwtConfiguration configuration = JwtConfiguration.builder().includeDetails(true).build(); // <1> Authentication authc = Authentication.builder("test").withParameter("name", "John").build(); // <2> String jwt = JwtTokenBuilder.get().buildJwt(configuration, authc); // <3> // end::build3[] }
public void build2() throws IOException { // tag::build2[] JwtConfiguration configuration = JwtConfiguration.builder().includePermissions(true).build(); // <1> Authentication authc = Authentication.builder("test").withPermission("role1").withPermission("role2").build(); // <2> String jwt = JwtTokenBuilder.get().buildJwt(configuration, authc); // <3> // end::build2[] }
public void authentication() { // tag::authentication[] Authentication authc = Authentication.builder("userId") // <1> .withPermission("VIEW") // <2> .withPermission(new MyPermission()) // <3> .withParameter("name", "John") // <4> .withParameter("surname", "Doe") // <5> .scheme("myscheme") // <6> .build(); // end::authentication[] }