日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了正確配置Spring安全OAuth2的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在嘗試使用Spring-Security-OAuth2和JWT配置授權(quán)服務(wù)器。

我的Main:

@SpringBootApplication
@EnableResourceServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我的安全配置:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    @Bean
    public AuthenticationManager    authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("john").password("123").roles("USER").authorities("FOO_READ");
    }
}

我的身份驗(yàn)證服務(wù)器配置:

@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("gateway")
                .secret("secret")
                .authorizedGrantTypes("refresh_token", "password")
                .scopes("GATEWAY")
                .autoApprove(true) ;
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenEnhancer(jwtTokenEnhancer()).authenticationManager(authenticationManager);
    }

}

和用于訪問(wèn)當(dāng)前用戶的Web服務(wù):

@RestController
public class UserController {

    @GetMapping("/users/me")
    public Principal user(Principal principal) {
        return principal;
    }
}

現(xiàn)在,如果我這樣做POST請(qǐng)求:

gateway:secret@localhost:10101/oauth/token
grant_type : password
username : john
password : 123

我得到的回復(fù)如下:

{
    "access_token": "...access token...",
    "token_type": "bearer",
    "refresh_token": "...refresh token...",
    "expires_in": 43199,
    "scope": "GATEWAY",
    "jti": "...jti..."
}

如果我使用訪問(wèn)令牌呼叫我的用戶WS:

GET localhost:10101/users/me
Authorization : Bearer ...access token...

我獲得空響應(yīng)。

但如果我的安全配置中有這幾行:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers().antMatchers("/wtf");
    }

然后我獲得相應(yīng)的響應(yīng)。

有人能給我解釋一下這是怎么工作的嗎?如果沒(méi)有這一附加行,為什么無(wú)法識(shí)別我的用戶?

謝謝。

推薦答案

實(shí)際上是Spring安全問(wèn)題。
解決方案在此帖子中:

https://github.com/spring-projects/spring-security-oauth/issues/980

應(yīng)添加此批注才能正確調(diào)用適配器:

@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 
    // ...
}

另一個(gè)解決方案是實(shí)現(xiàn)ResourceServerConfigurerAdapter而不是WebSecurityConfigurerAdapter。

這篇關(guān)于正確配置Spring安全OAuth2的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:OAuth2 Spring 正確 配置
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定