spring-boot - Spring Boot、Spring Security - 基于 XML 的配置
问题描述
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)?
解决方案
我尝试了以下
WebSecurityConfig.java
@Configuration
@ImportResource({ "classpath:webSecurityConfig.xml" })
public class WebSecurityConfig {
public WebSecurityConfig() {
super();
}
}
webSecurityConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<http create-session="stateless" use-expressions="true">
<intercept-url pattern="/" access="permitAll()"/>
<intercept-url pattern="/login" access="permitAll()"/>
<intercept-url pattern="/path1/**" access="hasAuthority('ROLE_ADMIN')"/>
<intercept-url pattern="/path2/**" access="hasAuthority('ROLE_USER')"/>
<intercept-url method="DELETE" pattern="/path3/{name}" access="hasAuthority('ROLE_ADMIN')"/>
<intercept-url method="GET" pattern="/path3/{name}" access="hasAuthority('ROLE_USER')"/>
<http-basic/>
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="user" password="{noop}user123" authorities="ROLE_USER"/>
<user name="admin" password="{noop}admin456" authorities="ROLE_USER,ROLE_ADMIN"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
但是在启动时显示以下错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setObjectPostProcessor in org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter required a bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor' in your configuration.
不知道有什么问题或遗漏。到目前为止,谷歌并不是很有帮助。
推荐阅读
- python-3.x - clang 链接器,ld:未找到架构 x86_64 的符号
- php - 如何在 laravel 的 yajra 数据表中显示图像
- sql - 如何合并两个具有一对多关系的表
- sql - 当我们使用 group by 时是否可以访问组中的行
- linux - 我想输出“
杀死 ~" 当它杀死 -9 时记录到日志文件 - php - 如何在 updateOrCreate eloquent 中添加 laravel 查询函数列表
- c++ - 数组中的ofstream一直写入同一个文件
- java - 如何从多行字符串中删除特定行
- ssis - 加载包失败
- python - 从 json 创建 python 对象