SpringOAuth2和摘要认证将协同工作
我已经将SpringOAuth2添加到我的restful服务中。大多数服务都由我自己的门户使用,因此获取令牌然后调用api就可以了。然而,我已经公开了一些更多的web服务,它们必须在没有这个令牌概念的情况下被调用。这些消费者有用户名和密码 最好的例子是招摇过市的实现。在这里,应该通过摘要而不是oauth令牌对打开swagger页面进行身份验证 下面的代码我做了更改,但它不工作。 我相信,在这种情况下,我不需要从资源服务器调用oauth服务器。因此,只需在资源服务器中编写以下代码。但是,在身份验证页面接受我的凭据后,它再次重新路由/重定向到同一身份验证页面 请帮忙SpringOAuth2和摘要认证将协同工作,spring,spring-security,spring-oauth2,Spring,Spring Security,Spring Oauth2,我已经将SpringOAuth2添加到我的restful服务中。大多数服务都由我自己的门户使用,因此获取令牌然后调用api就可以了。然而,我已经公开了一些更多的web服务,它们必须在没有这个令牌概念的情况下被调用。这些消费者有用户名和密码 最好的例子是招摇过市的实现。在这里,应该通过摘要而不是oauth令牌对打开swagger页面进行身份验证 下面的代码我做了更改,但它不工作。 我相信,在这种情况下,我不需要从资源服务器调用oauth服务器。因此,只需在资源服务器中编写以下代码。但是,在身份验证
@Configuration
@EnableResourceServer
public class ResourceServerImpl extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.anonymous().disable().requestMatchers().antMatchers("/**").and().authorizeRequests()
.antMatchers(HttpMethod.POST, "/url1/path1/path2").hasAnyAuthority( "FUNCTION_FN1")
.antMatchers(HttpMethod.GET, "/url2/path1/path2").hasAnyAuthority( "FUNCTION_FN2")
.antMatchers("/swagger-ui.html").hasRole("USER")
.anyRequest().authenticated()
.and().formLogin().and().httpBasic()
.and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
}
@Configuration
@EnableWebSecurity
public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
}
更新
我认为这是不可能的,因为有些请求是通过DIGEST进行身份验证的,有些请求是通过OAUTH进行身份验证的。因此,目前我使用oauth令牌对swagger url进行身份验证,而不是使用摘要支持。是的,在同一个应用程序中配置多个授权机制是完全可能的。应为每个机制提供
websecurityConfigureAdapter
的不同实例。
@EnableResourceServer
和@EnableAuthorizationServer
提供相应的oauth适配器<代码>ApiSecurityConfiguration为基本身份验证提供适配器
下面是basic
和oauth2
的最小工作示例:
@SpringBootApplication
public class TestApp {
@RestController
public static class Endpoints {
// doesn't require any authentication
@RequestMapping("/test")
public String test() {
return "no auth";
}
// requires basic authentication
@RequestMapping("/api/test")
public String apiTest() {
return "basic auth";
}
// requires oauth authentication
@RequestMapping("/oauth/test")
public String oauthTest() {
return "oauth";
}
}
@Configuration
@EnableWebSecurity
public static class ApiSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers().antMatchers("/api/**")
.and()
.authorizeRequests().antMatchers("/api/**").hasRole("USER")
.and()
.httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("USER");
}
}
@Configuration
@EnableAuthorizationServer
public static class AuthServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("test")
.secret("test")
.authorizedGrantTypes("client_credentials")
.authorities("USER")
.scopes("read", "write", "test")
.resourceIds("oauth2-resource")
.accessTokenValiditySeconds(120);
}
}
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatchers().antMatchers("/oauth/**")
.and()
.authorizeRequests().antMatchers("/oauth/**").authenticated();
}
}
public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
}
}
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
org.springframework.boot
springbootmaven插件
org.springframework.security.oauth
spring-security-oauth2
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动安全
你没有提供任何关于配置的代码,那么为什么你会问我必须做哪些代码更改?@AtaurRahmanMunna当然,我会提供,但这可行吗…?但是我已经公开了更多的web服务——因此其他用户和oauth2用户的端点不同,对吗?是的,Ataur。URL不同,但服务是单一的,但我希望在这两种情况下遵循相同的访问规则(授权)。您需要为httpBasic()提供单独的配置。提及-