@Override public String getName() { return authentication.getName(); }
public void authContext1() { // tag::authcontext1[] AuthContext authContext = SpringSecurity.authContext(); // <1> UsernamePasswordAuthenticationToken tkn = new UsernamePasswordAuthenticationToken("user", "pwd", Arrays.asList(new GrantedAuthority[] { new SimpleGrantedAuthority("role1") })); SecurityContextHolder.getContext().setAuthentication(tkn); // <2> Authentication authc = authContext.requireAuthentication(); // <3> String name = authc.getName(); // <4> boolean permitted = authContext.isPermitted("role1"); // <5> SecurityContextHolder.getContext().setAuthentication(null); // <6> boolean notAnymore = authContext.isAuthenticated(); // end::authcontext1[] }
public void authenticationListener() { // tag::listener[] Realm realm = getRealm(); realm.addAuthenticationListener(authentication -> { // <1> // do something ... authentication.getName(); }); // end::listener[] }
@Authenticate @GET @Path("name") @Produces(MediaType.TEXT_PLAIN) public String getPrincipalName(@javax.ws.rs.core.Context SecurityContext securityContext) { JaxrsAuthenticationInspector inspector = JaxrsAuthenticationInspector.of(securityContext); // <1> boolean isAuthenticated = inspector.isAuthenticated(); // <2> Optional<Authentication> auth = inspector.getAuthentication(); // <3> Authentication authc = inspector.requireAuthentication(); // <4> boolean permitted = inspector.isPermitted("ROLE1"); // <5> permitted = inspector.isPermittedAny("ROLE1", "ROLE2"); // <6> return inspector.getAuthentication().map(a -> a.getName()).orElse(null); } // end::authinsp[]
JwtBuilder builder = createJWT(id, authentication.getName(), issuer, (timeToLiveMs != null) ? timeToLiveMs.longValue() : -1, notBeforeNow);