Spring 启动应用程序可以为 REST API 提供单独的安全性吗?
Can Spring Boot application have separate security for REST APIs?
我们想为 Rest 控制器应用基于 Oauth2 的安全性,而应用程序的其余部分将具有 Spring 安全性。那可能吗?你能提供任何例子吗?
似乎 WebSecurityConfigurerAdapter 和 ResourceServerConfigurerAdapter 在配置时发生冲突。
提前致谢。
有可能。这里给出示例模板配置代码。请根据您的需要更改所需的配置。 关键是用不同的顺序定义配置的子静态类。 在这里我考虑了从\api
发起的任何请求作为REST API 打电话。
我没有通过编译检查代码
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Order(1)
@Configuration
public static class ApiWebSecurityConfig extends OAuth2ServerConfigurerAdapter{
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//Write the AuthenticationManagerBuilder codes for the OAuth
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.antMatcher("/api/**")
.authorizeRequests()
.anyRequest().authenticated()
.and()
.apply(new OAuth2ServerConfigurer())
.tokenStore(new InMemoryTokenStore())
.resourceId(applicationName);
}
}
}
@Order(2)
@Configuration
public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
//Write the AuthenticationManagerBuilder codes for the Normal authentication
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() //HTTP with Disable CSRF
.authorizeRequests() //Authorize Request Configuration
.anyRequest().authenticated()
.and() //Login Form configuration for all others
.formLogin()
.loginPage("/login").permitAll()
.and() //Logout Form configuration
.logout().permitAll();
}
}
}
我们想为 Rest 控制器应用基于 Oauth2 的安全性,而应用程序的其余部分将具有 Spring 安全性。那可能吗?你能提供任何例子吗?
似乎 WebSecurityConfigurerAdapter 和 ResourceServerConfigurerAdapter 在配置时发生冲突。
提前致谢。
有可能。这里给出示例模板配置代码。请根据您的需要更改所需的配置。 关键是用不同的顺序定义配置的子静态类。 在这里我考虑了从\api
发起的任何请求作为REST API 打电话。
我没有通过编译检查代码
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Order(1)
@Configuration
public static class ApiWebSecurityConfig extends OAuth2ServerConfigurerAdapter{
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//Write the AuthenticationManagerBuilder codes for the OAuth
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.antMatcher("/api/**")
.authorizeRequests()
.anyRequest().authenticated()
.and()
.apply(new OAuth2ServerConfigurer())
.tokenStore(new InMemoryTokenStore())
.resourceId(applicationName);
}
}
}
@Order(2)
@Configuration
public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
//Write the AuthenticationManagerBuilder codes for the Normal authentication
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() //HTTP with Disable CSRF
.authorizeRequests() //Authorize Request Configuration
.anyRequest().authenticated()
.and() //Login Form configuration for all others
.formLogin()
.loginPage("/login").permitAll()
.and() //Logout Form configuration
.logout().permitAll();
}
}
}