Spring security Spring引导应用程序是否可以为REST API提供单独的安全性?
我们希望为Rest控制器应用基于Oauth2的安全性,而应用程序的其余部分将具有Spring安全性。有可能吗?你能提供一些例子吗 WebSecurity配置适配器和ResourceServerConfigurerAdapter在配置时似乎发生冲突Spring security Spring引导应用程序是否可以为REST API提供单独的安全性?,spring-security,Spring Security,我们希望为Rest控制器应用基于Oauth2的安全性,而应用程序的其余部分将具有Spring安全性。有可能吗?你能提供一些例子吗 WebSecurity配置适配器和ResourceServerConfigurerAdapter在配置时似乎发生冲突 提前谢谢。是的,有可能。这里给出了示例模板配置代码。请根据需要更改所需配置关键是以不同的顺序定义配置的子静态类。这里我将源自\api的任何请求视为REST api调用 我没有通过编译来检查代码 是的,这是可能的。这里给出了示例模板配置代码。请根据需要更
提前谢谢。是的,有可能。这里给出了示例模板配置代码。请根据需要更改所需配置关键是以不同的顺序定义配置的子静态类。这里我将源自
\api
的任何请求视为REST api调用
我没有通过编译来检查代码
是的,这是可能的。这里给出了示例模板配置代码。请根据需要更改所需配置关键是以不同的顺序定义配置的子静态类。这里我将源自
\api
的任何请求视为REST api调用
我没有通过编译来检查代码
没有必要使用静态类。@Order注释即使在单独的文件中也可以使用。@HajderRabiee是的,它肯定可以使用。:)这是我的编码风格。您可以拥有自己的。@FarajFarook我收回:)当我有两个单独的类(2.java文件)时,我的应用程序似乎没有选择两个不同的身份验证提供程序。我现在有两个静态子类。在我的应用程序中,我有两个独立的类,它的工作原理不需要静态类。@Order注释即使在单独的文件中也可以使用。@HajderRabiee是的,它肯定可以使用。:)这是我的编码风格。您可以拥有自己的。@FarajFarook我收回:)当我有两个单独的类(2.java文件)时,我的应用程序似乎没有选择两个不同的身份验证提供程序。我现在有两个静态子类。在我的应用程序中,我有两个独立的类及其工作原理
@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();
}
}
}