我正在尝试在springboot中实现Basic Auth + oAuth2, 表示某些网址在登录系统后应该像传统方式一样工作,有些应该在AOuth2上工作。
就像我想允许管理面板访问SuperAdmin一样,url从
开始/超级管理员/ ****
我只想在一般登录系统后访问所有这些网址。
和Rest服务应该在AOuth2上使用url starts form
/ API / VI / ****
这些网址用于访问申请人。
另外两者都工作正常,但两者都不起作用。
这是我的配置。
import in.kpis.tracking.configuration.CustomAuthenticationSuccessHandler;
import in.kpis.tracking.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
@Configuration
public class OAuth2ServerConfiguration {
protected static final String RESOURCE_ID = "restservice";
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
// @formatter:off
resources.resourceId(RESOURCE_ID);
// @formatter:on
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/v1/*").hasRole("ADMIN")
.antMatchers("/greeting").authenticated();
}
}
@Configuration
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Autowired
private AdminUserService adminUserService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(adminUserService);
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
@Configuration
@Order(1)
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
String[] permitAll = new String[]{"/error"};
String[] permitToSuperAdmin = new String[]{
"/superAdmin/*",
};
http.authorizeRequests()
.antMatchers(permitToSuperAdmin).access("hasRole('SUPER_ADMIN')")
.antMatchers("/login").permitAll()
.and().formLogin().loginPage("/userLogin.html")
.usernameParameter("username")
.passwordParameter("password")
.loginProcessingUrl("/login")
.successHandler(new CustomAuthenticationSuccessHandler())
.and()
.logout().logoutSuccessUrl("/userLogin.html?logout")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true);
http.csrf().disable();
}
}
}
答案 0 :(得分:5)
如果您需要为应用程序的不同部分设置不同的安全设置,则需要创建单独的Spring Security @Configuration
- s,其中每个安全设置只配置一种身份验证机制。每个配置都应指定它所涵盖的URI,并且配置需要@Order
- ed。没有@Order注释的配置被认为是最后一个 - 后备。它是Spring Security参考手册中的described。
所以你需要三种配置:
http.antMatcher("/superAdmin/**")...
@Order(1)
。http.antMatcher("/api/vi/**")...
的{{1}}。@Order(2)
注释的其他资源的身份验证的后备配置。答案 1 :(得分:2)
“ Spring安全指南”在Multiple HttpSecurity下对此进行了解释
@Configuration
@Order(1) 2
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**") 3
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and()
.httpBasic();
}
}
@Configuration 4
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
要使用“资源服务器”保护OAuth2端点安全,您可以按以下步骤配置资源服务器
@Configuration
@EnableResourceServer
@Order(1)
protected static class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("resource-id");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatcher(new OAuthRequestedMatcher())
.authorizeRequests().anyRequest().fullyAuthenticated();
}
}
private static class OAuthRequestedMatcher implements RequestMatcher {
public boolean matches(HttpServletRequest request) {
String auth = request.getHeader("Authorization");
boolean haveOauth2Token = (auth != null) && auth.startsWith("Bearer");
boolean haveAccessToken = request.getParameter("access_token")!=null;
return haveOauth2Token || haveAccessToken;
}
}
答案 2 :(得分:0)
很好的问题 为了使用具有弹簧安全性的oAuth,我认为没有任何方法可以使用它。 你需要创建两个不同的项目,一个是一般的。一个是oAuth。