-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b28eb4
commit 671fc02
Showing
7 changed files
with
82 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...in/groovy/grails/plugin/springsecurity/rest/SpringSecurityRestFilterRequestMatcher.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package grails.plugin.springsecurity.rest | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Slf4j | ||
import org.springframework.security.web.util.matcher.RequestMatcher | ||
|
||
import javax.servlet.http.HttpServletRequest | ||
|
||
/** | ||
* Determines whether a given request matches against a configured endpoint URL | ||
* | ||
* @author Álvaro Sánchez-Mariscal | ||
*/ | ||
@Slf4j | ||
@CompileStatic | ||
class SpringSecurityRestFilterRequestMatcher implements RequestMatcher { | ||
|
||
private String endpointUrl | ||
|
||
SpringSecurityRestFilterRequestMatcher(String endpointUrl) { | ||
this.endpointUrl = endpointUrl | ||
} | ||
|
||
@Override | ||
boolean matches(HttpServletRequest request) { | ||
String actualUri = request.requestURI - request.contextPath | ||
log.debug "Actual URI is ${actualUri}; endpoint URL is ${endpointUrl}" | ||
return actualUri == endpointUrl | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...roovy/grails/plugin/springsecurity/rest/SpringSecurityRestFilterRequestMatcherSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package grails.plugin.springsecurity.rest | ||
|
||
import org.springframework.mock.web.MockHttpServletRequest | ||
import org.springframework.mock.web.MockServletContext | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
class SpringSecurityRestFilterRequestMatcherSpec extends Specification { | ||
|
||
@Unroll | ||
void "if the context path is #contextPath, the URI #uri matches: #matches"(String contextPath, String uri, boolean matches) { | ||
given: | ||
SpringSecurityRestFilterRequestMatcher requestMatcher = new SpringSecurityRestFilterRequestMatcher("/api/login") | ||
MockServletContext servletContext = new MockServletContext() | ||
servletContext.setContextPath(contextPath) | ||
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "POST", uri) | ||
|
||
when: | ||
boolean matchesResult = requestMatcher.matches(request) | ||
|
||
then: | ||
matchesResult == matches | ||
|
||
where: | ||
contextPath | uri || matches | ||
"/" | "/api/login" || true | ||
"/" | "/api/loginxxx" || false | ||
"/foo" | "/api/login/foo" || false | ||
} | ||
|
||
} |