[英]Spring oauth2 and digest authentication will work together
我已經將spring oauth2添加到了我的靜態服務中。 大多數服務都由我自己的門戶使用,因此獲取令牌然后調用api很好。 但是,我已經公開了一些沒有此令牌概念就必須調用的其他Web服務。 這些使用者具有用戶名和密碼。
最好的例子是Swagger實現。 在打開打開頁面時,應通過摘要而不是oauth令牌進行身份驗證。
我進行了以下代碼更改,但無法正常工作。 我相信,在這種情況下,我無需從資源服務器調用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。 因此,目前我也將swagger url認證為oauth令牌,而不是摘要支持。
是的,在同一個應用程序中肯定可以配置多種授權機制。 應該為每種機制提供WebSecurityConfigurerAdapter
不同實例。 @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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.