I'm trying to set up a Spring Authorization Server for learning purposes. The login and consent screens work, but after I approve the consent screen and submit the /authorize request, I get the following error:
16:47:19.849 [http-nio-8080-exec-3] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
java.lang.IllegalArgumentException: The class with dev.gaurav.auth_server.entity.User and name of dev.gaurav.auth_server.entity.User is not in the allowlist. If you believe this class is safe to deserialize, please provide an explicit mapping using Jackson annotations or by providing a Mixin. If the serialization is only done by a trusted source, you can also enable default typing. See https://github.com/spring-projects/spring-security/issues/4370 for details
at org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService$OAuth2AuthorizationRowMapper.parseMap(JdbcOAuth2AuthorizationService.java:648)
From what I understand:
Spring Authorization Server creates an
OAuth2Authorizationobject after consent approval.This object includes my custom
Userobject as part of the principal.Spring uses Jackson to serialize/deserialize the
OAuth2Authorizationobject to the database.Jackson refuses to deserialize my
Userclass because Spring Security 6+ has a strict allowlist for security reasons.
So the error happens after consent approval when Spring tries to persist or read the authorization.
My questions:
Why it worked with in-memory users but not JPA/JDBC: When I was using
InMemoryUserDetailsManager(for example, at the commit"Initialize OAuth 2.0 Authorization Server Project with Spring Boot"in my Git repo), everything worked fine.@Bean public UserDetailsService userDetailsService() { UserDetails userDetails = User.builder() .username("user") .passwordEncoder(passwordEncoder()::encode) .password("password") .roles("USER") .build(); return new InMemoryUserDetailsManager(userDetails); }How can I safely allow my custom
Userclass to be deserialized?Should I use a Mixin, Jackson annotations, or something else (which could also be a suitable choice for development, not just for learning purposes)?
Is there a recommended approach to avoid storing the full
Userobject inOAuth2Authorization.?