-
Notifications
You must be signed in to change notification settings - Fork 6k
Resource Server JWK support #5476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2f1e752
2ea1f06
7a52a07
6516508
51b75c1
2b628f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2013 the original author or authors. | ||
* Copyright 2002-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -128,7 +128,7 @@ public CsrfConfigurer<H> requireCsrfProtectionMatcher( | |
* </p> | ||
* | ||
* <p> | ||
* The following will ensure CSRF protection ignores: | ||
* For example, the following configuration will ensure CSRF protection ignores: | ||
* </p> | ||
* <ul> | ||
* <li>Any GET, HEAD, TRACE, OPTIONS (this is the default)</li> | ||
|
@@ -150,6 +150,35 @@ public CsrfConfigurer<H> ignoringAntMatchers(String... antPatterns) { | |
.and(); | ||
} | ||
|
||
/** | ||
* <p> | ||
* Allows specifying {@link HttpServletRequest}s that should not use CSRF Protection | ||
* even if they match the {@link #requireCsrfProtectionMatcher(RequestMatcher)}. | ||
* </p> | ||
* | ||
* <p> | ||
* For example, the following configuration will ensure CSRF protection ignores: | ||
* </p> | ||
* <ul> | ||
* <li>Any GET, HEAD, TRACE, OPTIONS (this is the default)</li> | ||
* <li>We also explicitly state to ignore any request that has a "X-Requested-With: XMLHttpRequest" header</li> | ||
* </ul> | ||
* | ||
* <pre> | ||
* http | ||
* .csrf() | ||
* .ignoringRequestMatchers(request -> "XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) | ||
* .and() | ||
* ... | ||
* </pre> | ||
* | ||
* @since 5.1 | ||
*/ | ||
public CsrfConfigurer<H> ignoringRequestMatchers(RequestMatcher... requestMatchers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create this as separate ticket and link to that ticket. Users will be interested to know about this separately. The separate commit can remain in this PR, but please make it a separate ticket and commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fyi - #5477 |
||
return new IgnoreCsrfProtectionRegistry(this.context).requestMatchers(requestMatchers) | ||
.and(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void configure(H http) throws Exception { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2013 the original author or authors. | ||
* Copyright 2002-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -23,6 +23,7 @@ | |
import org.springframework.security.web.access.AccessDeniedHandler; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update header to 2018 |
||
import org.springframework.security.web.access.AccessDeniedHandlerImpl; | ||
import org.springframework.security.web.access.ExceptionTranslationFilter; | ||
import org.springframework.security.web.access.RequestMatcherDelegatingAccessDeniedHandler; | ||
import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint; | ||
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint; | ||
import org.springframework.security.web.savedrequest.HttpSessionRequestCache; | ||
|
@@ -70,6 +71,8 @@ public final class ExceptionHandlingConfigurer<H extends HttpSecurityBuilder<H>> | |
|
||
private LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> defaultEntryPointMappings = new LinkedHashMap<>(); | ||
|
||
private LinkedHashMap<RequestMatcher, AccessDeniedHandler> defaultDeniedHandlerMappings = new LinkedHashMap<>(); | ||
|
||
/** | ||
* Creates a new instance | ||
* @see HttpSecurity#exceptionHandling() | ||
|
@@ -104,6 +107,26 @@ public ExceptionHandlingConfigurer<H> accessDeniedHandler( | |
return this; | ||
} | ||
|
||
/** | ||
* Sets a default {@link AccessDeniedHandler} to be used which prefers being | ||
* invoked for the provided {@link RequestMatcher}. If only a single default | ||
* {@link AccessDeniedHandler} is specified, it will be what is used for the | ||
* default {@link AccessDeniedHandler}. If multiple default | ||
* {@link AccessDeniedHandler} instances are configured, then a | ||
* {@link RequestMatcherDelegatingAccessDeniedHandler} will be used. | ||
* | ||
* @param deniedHandler the {@link AccessDeniedHandler} to use | ||
* @param preferredMatcher the {@link RequestMatcher} for this default | ||
* {@link AccessDeniedHandler} | ||
* @return the {@link ExceptionHandlingConfigurer} for further customizations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
* @since 5.1 | ||
*/ | ||
public ExceptionHandlingConfigurer<H> defaultAccessDeniedHandlerFor( | ||
AccessDeniedHandler deniedHandler, RequestMatcher preferredMatcher) { | ||
this.defaultDeniedHandlerMappings.put(preferredMatcher, deniedHandler); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the {@link AuthenticationEntryPoint} to be used. | ||
* | ||
|
@@ -169,13 +192,27 @@ public void configure(H http) throws Exception { | |
AuthenticationEntryPoint entryPoint = getAuthenticationEntryPoint(http); | ||
ExceptionTranslationFilter exceptionTranslationFilter = new ExceptionTranslationFilter( | ||
entryPoint, getRequestCache(http)); | ||
if (accessDeniedHandler != null) { | ||
exceptionTranslationFilter.setAccessDeniedHandler(accessDeniedHandler); | ||
} | ||
AccessDeniedHandler deniedHandler = getAccessDeniedHandler(http); | ||
exceptionTranslationFilter.setAccessDeniedHandler(deniedHandler); | ||
exceptionTranslationFilter = postProcess(exceptionTranslationFilter); | ||
http.addFilter(exceptionTranslationFilter); | ||
} | ||
|
||
/** | ||
* Gets the {@link AccessDeniedHandler} according to the rules specified by | ||
* {@link #accessDeniedHandler(AccessDeniedHandler)} | ||
* @param http the {@link HttpSecurity} used to look up shared | ||
* {@link AccessDeniedHandler} | ||
* @return the {@link AccessDeniedHandler} to use | ||
*/ | ||
AccessDeniedHandler getAccessDeniedHandler(H http) { | ||
AccessDeniedHandler deniedHandler = this.accessDeniedHandler; | ||
if (deniedHandler == null) { | ||
deniedHandler = createDefaultDeniedHandler(http); | ||
} | ||
return deniedHandler; | ||
} | ||
|
||
/** | ||
* Gets the {@link AuthenticationEntryPoint} according to the rules specified by | ||
* {@link #authenticationEntryPoint(AuthenticationEntryPoint)} | ||
|
@@ -191,16 +228,28 @@ AuthenticationEntryPoint getAuthenticationEntryPoint(H http) { | |
return entryPoint; | ||
} | ||
|
||
private AccessDeniedHandler createDefaultDeniedHandler(H http) { | ||
if (this.defaultDeniedHandlerMappings.isEmpty()) { | ||
return new AccessDeniedHandlerImpl(); | ||
} | ||
if (this.defaultDeniedHandlerMappings.size() == 1) { | ||
return this.defaultDeniedHandlerMappings.values().iterator().next(); | ||
} | ||
return new RequestMatcherDelegatingAccessDeniedHandler( | ||
this.defaultDeniedHandlerMappings, | ||
new AccessDeniedHandlerImpl()); | ||
} | ||
|
||
private AuthenticationEntryPoint createDefaultEntryPoint(H http) { | ||
if (defaultEntryPointMappings.isEmpty()) { | ||
if (this.defaultEntryPointMappings.isEmpty()) { | ||
return new Http403ForbiddenEntryPoint(); | ||
} | ||
if (defaultEntryPointMappings.size() == 1) { | ||
return defaultEntryPointMappings.values().iterator().next(); | ||
if (this.defaultEntryPointMappings.size() == 1) { | ||
return this.defaultEntryPointMappings.values().iterator().next(); | ||
} | ||
DelegatingAuthenticationEntryPoint entryPoint = new DelegatingAuthenticationEntryPoint( | ||
defaultEntryPointMappings); | ||
entryPoint.setDefaultEntryPoint(defaultEntryPointMappings.values().iterator() | ||
this.defaultEntryPointMappings); | ||
entryPoint.setDefaultEntryPoint(this.defaultEntryPointMappings.values().iterator() | ||
.next()); | ||
return entryPoint; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2016 the original author or authors. | ||
* Copyright 2002-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -18,7 +18,6 @@ | |
import java.util.ArrayList; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update header to 2018 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. For future reference, am I making the correct inference that whenever we touch a file, we update its license header? (This makes sense to me, I just don't want to misunderstand.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
|
@@ -105,7 +104,7 @@ public final class SessionManagementConfigurer<H extends HttpSecurityBuilder<H>> | |
private Integer maximumSessions; | ||
private String expiredUrl; | ||
private boolean maxSessionsPreventsLogin; | ||
private SessionCreationPolicy sessionPolicy = SessionCreationPolicy.IF_REQUIRED; | ||
private SessionCreationPolicy sessionPolicy; | ||
private boolean enableSessionUrlRewriting; | ||
private String invalidSessionUrl; | ||
private String sessionAuthenticationErrorUrl; | ||
|
@@ -549,7 +548,14 @@ AuthenticationFailureHandler getSessionAuthenticationFailureHandler() { | |
* @return the {@link SessionCreationPolicy} | ||
*/ | ||
SessionCreationPolicy getSessionCreationPolicy() { | ||
return this.sessionPolicy; | ||
if (this.sessionPolicy != null) { | ||
return this.sessionPolicy; | ||
} | ||
|
||
SessionCreationPolicy sessionPolicy = | ||
getBuilder().getSharedObject(SessionCreationPolicy.class); | ||
return sessionPolicy == null ? | ||
SessionCreationPolicy.IF_REQUIRED : sessionPolicy; | ||
} | ||
|
||
/** | ||
|
@@ -558,16 +564,18 @@ SessionCreationPolicy getSessionCreationPolicy() { | |
* @return true if the {@link SessionCreationPolicy} allows session creation | ||
*/ | ||
private boolean isAllowSessionCreation() { | ||
return SessionCreationPolicy.ALWAYS == this.sessionPolicy | ||
|| SessionCreationPolicy.IF_REQUIRED == this.sessionPolicy; | ||
SessionCreationPolicy sessionPolicy = getSessionCreationPolicy(); | ||
return SessionCreationPolicy.ALWAYS == sessionPolicy | ||
|| SessionCreationPolicy.IF_REQUIRED == sessionPolicy; | ||
} | ||
|
||
/** | ||
* Returns true if the {@link SessionCreationPolicy} is stateless | ||
* @return | ||
*/ | ||
private boolean isStateless() { | ||
return SessionCreationPolicy.STATELESS == this.sessionPolicy; | ||
SessionCreationPolicy sessionPolicy = getSessionCreationPolicy(); | ||
return SessionCreationPolicy.STATELESS == sessionPolicy; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update header to 2018