How to put a URL with a regular expression in Spri

2019-03-03 02:22发布

问题:

In a Spring Security XML configuration file, I have something like

<security:intercept-url pattern="\A/categories/\d+/items/admin\Z" access="ROLE_USER" />

How to handle the regular expression if I convert the above into a Java based configuration?

回答1:

You have to use regexMatchers method on HttpSecurity:

public class SpringSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .regexMatchers("\A/categories/\d+/items/admin\").hasRole("USER");
    }
}