【发布时间】:2021-02-15 00:46:11
【问题描述】:
Spring Boot 2.3.4,Spring Security,保护 REST 控制器(端点)。
我有一个使用 Java 配置类的解决方案,但现在我的任务是使用 XML 来完成。
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
public static final String USER = "USER";
public static final String ADMIN = "ADMIN";
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user")
.password("{noop}" + "user123")
.roles(USER)
.and()
.withUser("admin")
.password("{noop}" + "admin456")
.roles(ADMIN, USER);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/", "/login").permitAll()
.antMatchers("/path1/**").hasRole(ADMIN)
.antMatchers("/path2/**").hasRole(USER)
.antMatchers(HttpMethod.DELETE, "/path3/{name}").hasRole(ADMIN)
.antMatchers(HttpMethod.GET, "/path3/{name}").hasRole(USER)
// more antMatchers...
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable()
.formLogin().disable();
}
}
以前从未做过基于 XML 的配置,这一定是过去经常做的事情。不管怎样,得到了用 XML 完成的任务。
不知道怎么做。 需要哪些部分,只是 XML 文件,还是仍然是 Java 配置文件(如 WebSecurityConfig.java)?
【问题讨论】:
标签: spring-boot spring-security