Skip to content

Commit

Permalink
Handle a few more cases for HttpSecurity dsl updates
Browse files Browse the repository at this point in the history
  • Loading branch information
shanman190 committed Jul 27, 2023
1 parent 4d39725 commit f8186d5
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@
import org.openrewrite.Tree;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;
import org.openrewrite.marker.Markup;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;

public class ConvertToSecurityDslVisitor<P> extends JavaIsoVisitor<P> {

Expand Down Expand Up @@ -66,7 +71,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation initialMethod
}
Boolean msg = getCursor().pollMessage(MSG_FLATTEN_CHAIN);
if (Boolean.TRUE.equals(msg)) {
method = (J.MethodInvocation) method.getSelect();
method = requireNonNull(method.getSelect())
.withPrefix(method.getPrefix())
.withComments(method.getComments());
}
// Auto-format the top invocation call if anything has changed down the tree
if (initialMethod != method && (getCursor().getParent(2) == null || !(getCursor().getParent(2).getValue() instanceof J.MethodInvocation))) {
Expand Down Expand Up @@ -142,6 +149,9 @@ private boolean isApplicableCallCursor(Cursor c) {
if (!TypeUtils.isOfClassType(inv.getType(), securityFqn)) {
return true;
}
if (new MethodMatcher("org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer disable()").matches(inv)) {
return true;
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,128 @@ protected void configure(HttpSecurity http) throws Exception {
);
}

@Test
void handleDisableChain() {
rewriteRun(
//language=java
java(
"""
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class ConventionalSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
""",
"""
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class ConventionalSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable());
}
}
"""
)
);
}

@Test
void retainComments() {
rewriteRun(
//language=java
java(
"""
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class ConventionalSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// matcher order matters
http.authorizeRequests()
.antMatchers("/blog/**").permitAll()
.anyRequest().authenticated();
}
}
""",
"""
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class ConventionalSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// matcher order matters
http.authorizeRequests(requests -> requests
.antMatchers("/blog/**").permitAll()
.anyRequest().authenticated());
}
}
"""
)
);
}

@Test
void retainFormatting() {
rewriteRun(
//language=java
java(
"""
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class ConventionalSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/blog/**").permitAll()
.anyRequest().authenticated();
http.csrf().disable();
}
}
""",
"""
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class ConventionalSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests(requests -> requests
.antMatchers("/blog/**").permitAll()
.anyRequest().authenticated());
http.csrf(csrf -> csrf.disable());
}
}
"""
)
);
}
}

0 comments on commit f8186d5

Please sign in to comment.