My spring authorisation server depedency :-
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-authorization-server</artifactId>
<version>1.0.0</version>
</dependency>
SecurityConfig class for authorization server:-
public class SecurityConfig {
@Autowired
private JwtAuthenticationEntryPoint authenticationEntryPoint;
@Bean
@Order(1)
public SecurityFilterChain asSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
http.getConfigurer(OAuth2AuthorizationServerConfigurer.class)
.authorizationEndpoint(a -> a.authenticationProviders(getAuthorizationEndPoints()))
.oidc(Customizer.withDefaults());
http.exceptionHandling(exception -> exception
.authenticationEntryPoint(authenticationEntryPoint));
return http.build();
}
@Bean
@Order(2)
public SecurityFilterChain appSecurityFilterChain(HttpSecurity http) throws Exception {
http.csrf().disable();
http.cors(c -> c.configurationSource(
request -> {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin(request.getHeader("Origin"));
config.setAllowedMethods(List.of("GET", "POST", "DELETE", "LINK", "UNLINK", "PATCH", "PUT", "OPTIONS"));
config.setAllowedHeaders(List.of("Content-Type, Accept, X-Requested-With, remember-me"));
config.setAllowCredentials(true);
config.setExposedHeaders(List.of("Authorization"));
config.setMaxAge(3600L);
return config;
}
));
http
.httpBasic().disable()
.formLogin().disable()
.authorizeHttpRequests()
.requestMatchers("/authenticate").permitAll()
.anyRequest().authenticated();
return http.build();
}
}
Issue :-
- When i call below API http://localhost:7171/oauth2/authorize?response_type=code&client_id=client&scope=openid&redirect_uri=http://localhost:3000
it throws
2023-05-16T15:31:51.127+05:30 TRACE 63279 --- [nio-7171-exec-4] o.s.s.w.a.ExceptionTranslationFilter : Sending AnonymousAuthenticationToken [Principal=anonymousUser, Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=null], Granted Authorities=[ROLE_ANONYMOUS]] to authentication entry point since access is denied
org.springframework.security.access.AccessDeniedException: Access Denied at org.springframework.security.web.access.intercept.AuthorizationFilter.doFilter(AuthorizationFilter.java:98) ~[spring-security-web-6.0.3.jar:6.0.3]
When i am using formlogin(), it use to redirect to HTTP://localhost:7171/login and asks for username and password and returns authorization_code which needs to be entered in below "http://localhost:7171/oauth2/token " which gives OAuth2 token. How to integrate it with UI.
How to write custom authentication to authenticate and it will give the oauth2 token?
IN simple words, I am applying it without a spring-cloud-gateway just a simple implementation using authorization-server I am stuck at the point, when client app hits for authorisation through API like http://localhost:7171/oauth2/authorize?response_type=code&client_id=client&scope=openid&redirect_uri=http://localhost:3000
which will redirect to authorization server login page.
How can i configure authorization server in a way it will authenticate and provide authorization_code to client so that client can apply http://localhost:7171/oauth2/token to get auth token? That's the ask .
Your authorization server should be configured with form-login (it is its responsibility to authenticate users), and what is an OAuth2 client in your architecture should use
Authorization codeflow (with PKCE) to get tokens when users are involved.Your frontend should not have access to user credentials (contain form for login and password). Both
passwordandimplicitflows were deprecated for security reasons.The OAuth2 client should use a library to store tokens and handle all the redirections you are struggling with.
There are currently at least 2 options for defining what is the OAuth2 client:
Authorizationheader containing aBeareraccess token before forwarding a request from the frontend to resource server(s)The second options is now frequently preferred because:
HttpOnly)spring-cloud-gatewaycan be configured as BFF: withspring-boot-starter-oauth2-clientand using theTokenRelayfilter. I wrote a tutorial on Baeldung.