Skip to content

Commit

Permalink
Chain in AuthorizeHttpRequests with spring-security-58.yml (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Aug 23, 2023
1 parent 68f4ea4 commit 4791363
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.spring.boot2;
package org.openrewrite.java.spring.security5;

import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
Expand All @@ -26,16 +26,19 @@
import org.openrewrite.marker.Markers;

import java.util.ArrayList;
import java.util.List;

public class AuthorizeHttpRequests extends Recipe {

private static final String MSG_ADD_COMMENT = "add-comment";

private static final String AUTHORIZE_HTTP_REQUESTS = "authorizeHttpRequests";

private static final MethodMatcher MATCH_AUTHORIZE_REQUESTS = new MethodMatcher("org.springframework.security.config.annotation.web.builders.HttpSecurity authorizeRequests(..)");
private static final MethodMatcher MATCH_AUTHORIZE_REQUESTS = new MethodMatcher(
"org.springframework.security.config.annotation.web.builders.HttpSecurity authorizeRequests(..)");

private static final MethodMatcher MATCH_ACCESS_DECISION_MANAGER = new MethodMatcher("org.springframework.security.config.annotation.web.configurers.AbstractInterceptUrlConfigurer$AbstractInterceptUrlRegistry accessDecisionManager(..)");
private static final MethodMatcher MATCH_ACCESS_DECISION_MANAGER = new MethodMatcher(
"org.springframework.security.config.annotation.web.configurers.AbstractInterceptUrlConfigurer$AbstractInterceptUrlRegistry accessDecisionManager(..)");

@Override
public String getDisplayName() {
Expand All @@ -50,14 +53,21 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
{
doAfterVisit(new ChangeType("org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry", "org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer.AuthorizationManagerRequestMatcherRegistry", false).getVisitor());
doAfterVisit(new ChangeType("org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer", "org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer", false).getVisitor());
doAfterVisit(new ChangeType("org.springframework.security.config.annotation.web.configurers.AbstractInterceptUrlConfigurer", "org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer", false).getVisitor());
private void changeTypesAfterVisit(){
doAfterVisit(new ChangeType(
"org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry",
"org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer.AuthorizationManagerRequestMatcherRegistry",
false).getVisitor());
doAfterVisit(new ChangeType(
"org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer",
"org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer",
false).getVisitor());
doAfterVisit(new ChangeType(
"org.springframework.security.config.annotation.web.configurers.AbstractInterceptUrlConfigurer",
"org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer",
false).getVisitor());
}



@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J visited = super.visitMethodInvocation(method, ctx);
Expand All @@ -66,8 +76,10 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
JavaType.Method methodType = method.getMethodType();
if (methodType != null) {
if (MATCH_AUTHORIZE_REQUESTS.matches(methodType)) {
changeTypesAfterVisit();
return processAuthorizeRequests(m);
} else if (MATCH_ACCESS_DECISION_MANAGER.matches(methodType)) {
changeTypesAfterVisit();
return processAccessDecisionManager(m, ctx);
}
}
Expand All @@ -81,7 +93,10 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)

private J.MethodInvocation processAuthorizeRequests(J.MethodInvocation m) {
JavaType.Method methodType = m.getMethodType();
JavaType.Method newMethodType = methodType.getDeclaringType().getMethods().stream().filter(nm -> AUTHORIZE_HTTP_REQUESTS.equals(nm.getName()) && nm.getParameterTypes().size() == methodType.getParameterTypes().size()).findFirst().orElse(null);
JavaType.Method newMethodType = methodType.getDeclaringType().getMethods().stream()
.filter(nm -> AUTHORIZE_HTTP_REQUESTS.equals(nm.getName()))
.filter(nm -> nm.getParameterTypes().size() == methodType.getParameterTypes().size())
.findFirst().orElse(null);
if (newMethodType != null) {
m = m
.withName(m.getName().withSimpleName(AUTHORIZE_HTTP_REQUESTS))
Expand All @@ -98,7 +113,7 @@ private J processAccessDecisionManager(J.MethodInvocation m, ExecutionContext ct
commentText.append(String.join(", ", m.getArguments().stream().map(a -> a.print(getCursor())).toArray(String[]::new)));
commentText.append(");' with appropriate call to 'access(AuthorizationManager)' after antMatcher(...) call etc.");

ArrayList<Comment> newComments = new ArrayList<>(m.getComments());
List<Comment> newComments = new ArrayList<>(m.getComments());
newComments.addAll(m.getSelect().getComments());

Expression selectExpr = m.getSelect();
Expand All @@ -117,7 +132,7 @@ private J processAccessDecisionManager(J.MethodInvocation m, ExecutionContext ct
private J.MethodInvocation addTextCommentAfterSelect(J.MethodInvocation m, String s) {
J.MethodInvocation.Padding padding = m.getPadding();
Space afterSelect = padding.getSelect().getAfter();
ArrayList<Comment> newComments = new ArrayList<>(afterSelect.getComments());
List<Comment> newComments = new ArrayList<>(afterSelect.getComments());
newComments.add(new TextComment(true, s, newComments.isEmpty() ? "\n" + afterSelect.getIndent() : newComments.get(0).getSuffix(), Markers.EMPTY));
JRightPadded<Expression> paddedSelect = padding.getSelect().withAfter(afterSelect.withComments(newComments));
return new J.MethodInvocation(m.getId(), m.getPrefix(), m.getMarkers(), paddedSelect, padding.getTypeParameters(), m.getName(), padding.getArguments(), m.getMethodType());
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/spring-security-58.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ recipeList:
artifactId: "*"
newVersion: 5.8.x
overrideManagedVersion: false
- org.openrewrite.java.spring.security5.AuthorizeHttpRequests
- org.openrewrite.java.spring.security5.UseNewRequestMatchers
- org.openrewrite.java.spring.security5.UseNewSecurityMatchers
- org.openrewrite.java.spring.security5.UpdatePbkdf2PasswordEncoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.spring.boot2;
package org.openrewrite.java.spring.security5;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
Expand All @@ -23,7 +23,7 @@

import static org.openrewrite.java.Assertions.java;

public class AuthorizeHttpRequestsTest implements RewriteTest {
class AuthorizeHttpRequestsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
Expand Down

0 comments on commit 4791363

Please sign in to comment.