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

 

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,就无需登录,直接可以访问

猜你喜欢

转载自blog.csdn.net/qq_22037575/article/details/87858124