Skip to content

Commit

Permalink
gh-83 do not force http when SSL is not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4mpy committed Jan 3, 2023
1 parent 3ee1181 commit 0633d6d
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ public class SecurityConfig {
// If SSL enabled, disable http (https only)
if (serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled()) {
http.requiresChannel().anyRequest().requiresSecure();
} else {
http.requiresChannel().anyRequest().requiresInsecure();
}

// Route security: authenticated to all routes but actuator and Swagger-UI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ SecurityFilterChain filterChain(HttpSecurity http,
// If SSL enabled, disable http (https only)
if (serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled()) {
http.requiresChannel().anyRequest().requiresSecure();
} else {
http.requiresChannel().anyRequest().requiresInsecure();
}

// Route security: authenticated to all routes but actuator and Swagger-UI
Expand Down
2 changes: 0 additions & 2 deletions samples/tutorials/resource-server_with_ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ public class WebSecurityConfig {
// If SSL enabled, disable http (https only)
if (isSsl) {
http.requiresChannel().anyRequest().requiresSecure();
} else {
http.requiresChannel().anyRequest().requiresInsecure();
}

// compared to API filter-chain:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public class WebSecurityConfig {

/**
* <p>
* A default SecurityFilterChain is already defined by
* spring-addons-webmvc-jwt-resource-server to secure all API endpoints
* (actuator and REST controllers)
* A default SecurityFilterChain is already defined by spring-addons-webmvc-jwt-resource-server to secure all API endpoints (actuator and
* REST controllers)
* </p>
* We define here another SecurityFilterChain for server-side rendered pages:
* <ul>
Expand All @@ -34,22 +33,21 @@ public class WebSecurityConfig {
* <li>Thymeleaf pages served by UiController</li>
* </ul>
* <p>
* It important to note that in this scenario, the end-user browser is not an
* OAuth2 client. Only the part of the server-side part of the Spring
* application secured with this filter chain is. Requests between the browser
* and Spring OAuth2 client are secured with <b>sessions</b>. As so, <b>CSRF
* protection must be active</b>.
* It important to note that in this scenario, the end-user browser is not an OAuth2 client. Only the part of the server-side part of the
* Spring application secured with this filter chain is. Requests between the browser and Spring OAuth2 client are secured with
* <b>sessions</b>. As so, <b>CSRF protection must be active</b>.
* </p>
*
* @param http
* @param serverProperties
* @return an additional security filter-chain for UI elements (with OAuth2
* login)
* @param http
* @param serverProperties
* @return an additional security filter-chain for UI elements (with OAuth2 login)
* @throws Exception
*/
@Order(Ordered.HIGHEST_PRECEDENCE)
@Bean
SecurityFilterChain uiFilterChain(HttpSecurity http, ServerProperties serverProperties,
SecurityFilterChain uiFilterChain(
HttpSecurity http,
ServerProperties serverProperties,
Converter<Map<String, Object>, Collection<? extends GrantedAuthority>> authoritiesConverter)
throws Exception {
boolean isSsl = serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled();
Expand All @@ -64,7 +62,7 @@ SecurityFilterChain uiFilterChain(HttpSecurity http, ServerProperties serverProp
// and OAuth2 client callback endpoints
new AntPathRequestMatcher("/login/**"),
new AntPathRequestMatcher("/oauth2/**")));

http.oauth2Login()
// I don't know quite why we are redirected to authorization-server port by default as initial login page is generated on client :/
.loginPage("%s://localhost:%d/oauth2/authorization/spring-addons-public".formatted(isSsl ? "https" : "http", serverProperties.getPort()) )
Expand All @@ -78,7 +76,7 @@ SecurityFilterChain uiFilterChain(HttpSecurity http, ServerProperties serverProp
.map(OidcUserAuthority.class::cast)
.flatMap(oua -> authoritiesConverter.convert(oua.getIdToken().getClaims()).stream()).toList()
);

http.authorizeHttpRequests()
.requestMatchers("/login/**").permitAll()
.requestMatchers("/oauth2/**").permitAll()
Expand All @@ -88,8 +86,6 @@ SecurityFilterChain uiFilterChain(HttpSecurity http, ServerProperties serverProp
// If SSL enabled, disable http (https only)
if (isSsl) {
http.requiresChannel().anyRequest().requiresSecure();
} else {
http.requiresChannel().anyRequest().requiresInsecure();
}

// compared to API filter-chain:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ SecurityFilterChain filterChain(

if (serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled()) {
http.requiresChannel().anyRequest().requiresSecure();
} else {
http.requiresChannel().anyRequest().requiresInsecure();
}

expressionInterceptUrlRegistryPostProcessor.authorizeHttpRequests(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ SecurityFilterChain filterChain(

if (serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled()) {
http.requiresChannel().anyRequest().requiresSecure();
} else {
http.requiresChannel().anyRequest().requiresInsecure();
}

expressionInterceptUrlRegistryPostProcessor.authorizeHttpRequests(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ SecurityFilterChain filterChain(HttpSecurity http, ServerProperties serverProper

if (serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled()) {
http.requiresChannel().anyRequest().requiresSecure();
} else {
http.requiresChannel().anyRequest().requiresInsecure();
}

return http.build();
Expand Down

0 comments on commit 0633d6d

Please sign in to comment.