在 Spring Boot 中忽略某些端点的授权
Ignore authorization for some endpoints in Spring Boot
我有 OAuth2 授权的 OAuth 服务器和客户端。
现在,如果我需要调用我的服务,我需要:
使用以下方法从服务器生成访问令牌 API :
localhost:9191/oauth/token?grant_type=password&username=krish&password=kpass
给出的响应如下:
"access_token": "ee41435d-8ad9-432e-82c1-07477e2b6956",
"token_type": "bearer",
"refresh_token": "ea6d83b4-62f6-4eaf-9f89-8600bd36690d",
"expires_in": 3429,
"scope": "READ WRITE"
现在我将访问令牌传递给 运行 客户端服务,如下所示:
所以这是我手动做的。但是我需要从客户端代码中 运行 它。当我试图点击第一个 API 本身(服务器)来获取令牌时,它说未经授权。
我的服务代码如下:
我需要跳过 /getToken
控制器的身份验证。我怎样才能做到这一点?谁能帮忙
我的 WebSecurityConfigurerAdapter class 如下:
我在阅读下面的一个答案后添加了突出显示的代码,但这也不起作用。
您可能想要创建一个扩展 WebSecurityConfigurerAdapter 的新配置并覆盖配置方法。
看看这个 guide 的实际例子。
你要关注的是这部分
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
如您在示例中所见,路径“/login”、“/”、“/home”被排除在身份验证之外。
还要检查其他答案:Spring Security exclude url patterns in security annotation configurartion
我有 OAuth2 授权的 OAuth 服务器和客户端。
现在,如果我需要调用我的服务,我需要:
使用以下方法从服务器生成访问令牌 API :
localhost:9191/oauth/token?grant_type=password&username=krish&password=kpass
给出的响应如下:
"access_token": "ee41435d-8ad9-432e-82c1-07477e2b6956", "token_type": "bearer", "refresh_token": "ea6d83b4-62f6-4eaf-9f89-8600bd36690d", "expires_in": 3429, "scope": "READ WRITE"
现在我将访问令牌传递给 运行 客户端服务,如下所示:
所以这是我手动做的。但是我需要从客户端代码中 运行 它。当我试图点击第一个 API 本身(服务器)来获取令牌时,它说未经授权。
我的服务代码如下:
我需要跳过 /getToken
控制器的身份验证。我怎样才能做到这一点?谁能帮忙
我的 WebSecurityConfigurerAdapter class 如下: 我在阅读下面的一个答案后添加了突出显示的代码,但这也不起作用。
您可能想要创建一个扩展 WebSecurityConfigurerAdapter 的新配置并覆盖配置方法。 看看这个 guide 的实际例子。 你要关注的是这部分
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
如您在示例中所见,路径“/login”、“/”、“/home”被排除在身份验证之外。 还要检查其他答案:Spring Security exclude url patterns in security annotation configurartion