springboot后台服务搭建(八 整合security + oauth2)

总览:https://blog.csdn.net/qq_22037575/article/details/86687765

本文概要:springboot2.x 整合 security + oauth2

码云:https://gitee.com/RichterGit/csdn/tree/master/springboot-radmin/008/

目录

1.pom导入依赖

2.全局配置

3.获取 token

4.刷新 token 

5.校验 token

6.通过 token 访问资源


1.pom导入依赖

 

<dependency>
    <groupId>org.springframework.security.oauth.boot</groupId>
    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

 2.全局配置

 修改配置类 WebSecurityConfig,把 AuthenticationManager放到 bean容器中

新建一个认证配置类 AuthServerConfig 

 

在 AuthServerConfig需要用到在 WebSecurityConfig的AUthenticationManager

并且重写一下几个方法

tokenKeyAccess("permitAll()"):访问“/oauth/token_key” 端口无需认证

checkTokenAccess("isAuenticated()"):访问“/oauth/check_token” 端口需要认证

@Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        // super.configure(security);
        security
                .passwordEncoder(passwordEncoder)
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }
@Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // super.configure(clients);
        clients
                .inMemory()
                .withClient("browser")
                .authorizedGrantTypes("password", "refresh_token")
                .scopes("ui");
    }
@Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        // super.configure(endpoints);
        endpoints
                .tokenStore(new InMemoryTokenStore())
                .authenticationManager(authenticationManager)
                .userDetailsService(userDetailServiceImpl);
    }

 3.获取 token

 通过 Postman 访问

http://localhost:8080/radmin/oauth/token?grant_type=password&scope=ui&username=admin&password=admin

Headers中的 Authorization是存放 Basic + (AuthServerConfig中 clientId 和 secret 经过 base64编码的字符串)

@Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // super.configure(clients);
        clients
                .inMemory()
                .withClient("browser")
                .authorizedGrantTypes("password", "refresh_token")
                .scopes("ui");
    }

4.刷新 token 

通过 Postman 访问

refresh_token对应上面获取 token后返回的刷新token

"refresh_token": "df81b083-9467-4be8-84fd-9d6f48ebc731"

http://localhost:8080/radmin/oauth/token?grant_type=refresh_token&refresh_token=df81b083-9467-4be8-84fd-9d6f48ebc731

5.校验 token

通过 Postman 访问

这里要注意 token 是已经刷新完成后的 access_token,最初获取 token的 access_token已经失效

"access_token": "96ca8e7b-a0ee-45b9-b183-03dfba22e072"

http://localhost:8080/radmin/oauth/check_token?token=96ca8e7b-a0ee-45b9-b183-03dfba22e072

6.通过 token 访问资源

修改 WebSecurityConfig的 configure(HttpSecurity http)

新建一个全局资源配置类 ResourceServerCOnfig

@Override
    protected void configure(HttpSecurity http) throws Exception {
        // super.configure(http);
        http
                .csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .httpBasic();
    }

 通过 Postman 获取 token

"access_token": "f91c66ba-fc7b-436a-9a76-8671f958b99f"

 访问 http://localhost:8080/radmin/role/findAll

因为未登录,访问受保护的资源,会出现无权限访问问题,并且这里不再是自动跳转到 security默认的登录页面

访问 http://localhost:8080/radmin/role/findAll?access_token=f91c66ba-fc7b-436a-9a76-8671f958b99f 

在访问路径后面追加 access_token

access_token后面的 token就是通过 "/oauth/token" 获取的,当请求服务器受保护的资源时,带上令牌 token,就无需登录,直接可以访问

 

全部评论

相关推荐

03-30 21:02
已编辑
武汉大学 Java
ALEX_BLX:虽然说聊天记录不可信,不过这个趋势确实如此但我觉得也要想到一点就是卷后端的人里真正有“料”的人又有多少,我说的这个料都不是说一定要到大佬那种级别,而是就一个正常的水平。即使是现在也有很多人是跟风转码的,2-3个月速成后端技术栈的人数不胜数,但今时不同往日没可能靠速成进大厂了。这种情况就跟考研一样,你能上考场就已经打败一半的人了
点赞 评论 收藏
分享
04-09 09:47
门头沟学院 Java
Arbelite_:2-3k,这工资还不如去摇奶茶
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务