Spring Security filters for JWT-based authentication, verification and authorization scheme, by example

As I understand it, you want to:

  1. Authenticate users via a username and password and respond with a JWT
  2. On subsequent requests, authenticate users using that JWT

username/password -> JWT isn’t an established authentication mechanism on its own, which is why Spring Security doesn’t yet have direct support.

You can get it on your own pretty easily, though.

First, create a /token endpoint that produces a JWT:

public class TokenController {

    RSAPrivateKey key;

    public String token(Authentication authentication) {
        Instant now = Instant.now();
        long expiry = 36000L;
        // @formatter:off
        String scope = authentication.getAuthorities().stream()
                .collect(Collectors.joining(" "));
        JWTClaimsSet claims = new JWTClaimsSet.Builder()
                .issueTime(new Date(now.toEpochMilli()))
                .expirationTime(new Date(now.plusSeconds(expiry).toEpochMilli()))
                .claim("scope", scope)
        // @formatter:on
        JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).build();
        SignedJWT jwt = new SignedJWT(header, claims);
        return sign(jwt).serialize();

    SignedJWT sign(SignedJWT jwt) {
        try {
            jwt.sign(new RSASSASigner(this.key));
            return jwt;
        catch (Exception ex) {
            throw new IllegalArgumentException(ex);


Second, configure Spring Security to allow HTTP Basic (for the /token endpoint) and JWT (for the rest):

public class RestConfig extends WebSecurityConfigurerAdapter {

    RSAPublicKey key;

    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.authorizeRequests((authz) -> authz.anyRequest().authenticated())
            .csrf((csrf) -> csrf.ignoringAntMatchers("/token"))
            .sessionManagement((session) -> session
            .exceptionHandling((exceptions) -> exceptions
                .authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint())
                .accessDeniedHandler(new BearerTokenAccessDeniedHandler())
        // @formatter:on

    UserDetailsService users() {
        // @formatter:off
        return new InMemoryUserDetailsManager(
        // @formatter:on

    JwtDecoder jwtDecoder() {
        return NimbusJwtDecoder.withPublicKey(this.key).build();


I think there’s appetite to add support for something like this in spring-authorization-server to reduce the /token boilerplate, if you’re interested in contributing your efforts!

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top