【SpringSecurityOAuth2客户端模式集成Springboot项目】

SpringSecurityOAuth2客户端模式集成Springboot项目

一、导入maven依赖

        <!--oauth-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>2.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth.boot</groupId>
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
            <version>2.3.4</version>
        </dependency>

二、配置类继承AuthorizationServerConfigurerAdapter

/**
 * 认证授权配置
 * @author ry
 * @date 2021/11/17 14:15
 */
@Configuration
@EnableAuthorizationServer
public class Oauth2ServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private AuthenticationManager authenticationManager;

    /**
     * 从数据库中查询出客户端信息
     * @return
     */
    @Bean
    public JdbcClientDetailsService clientDetailsService() {
        JdbcClientDetailsService detailsService = new JdbcClientDetailsService(dataSource);
        detailsService.setPasswordEncoder(new BCryptPasswordEncoder());
        return detailsService;
    }

    /**
     * token保存至数据库
     * @return
     */
    @Bean
    public TokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
        // 如要保存至内存可以切换
        //return new InMemoryTokenStore();
    }

    /**
     * 授权信息保存至数据库
     * @return
     */
    @Bean
    public ApprovalStore approvalStore() {
        return new JdbcApprovalStore(dataSource);
    }

    /**
     * 授权码模式专用对象
     * @return
     */
    //@Bean
    //public AuthorizationCodeServices authorizationCodeServices() {
    //    return new JdbcAuthorizationCodeServices(dataSource);
    //}

    /**
     * 指定客户端登录信息来源
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //从数据库取数据
        clients.withClientDetails(clientDetailsService());
    }

    /**
     * 检测token的策略
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //允许form表单客户端认证,允许客户端使用client_id和client_secret获取token
        security.allowFormAuthenticationForClients()
                //通过验证返回token信息
                .checkTokenAccess("isAuthenticated()")
                // 获取token请求不进行拦截
                .tokenKeyAccess("permitAll()")
                // 允许表单认证
                .allowFormAuthenticationForClients()
                .passwordEncoder(new BCryptPasswordEncoder());
    }

    /**
     * OAuth2的主配置信息
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                //指定授权信息保存至数据库
                .approvalStore(approvalStore())
                
                //指定授权管理器
                .authenticationManager(authenticationManager)
                
                //授权码模式使用
                //.authorizationCodeServices(authorizationCodeServices())
                //指定客户端token保存位置等信息
                .tokenServices(tokenServices())
                //指定请求方式
                .allowedTokenEndpointRequestMethods(HttpMethod.POST);
                //.reuseRefreshTokens(true);
    }
    @Bean
    public AuthorizationServerTokenServices tokenServices(){
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        //设置客户端详情
        tokenServices.setClientDetailsService(clientDetailsService());
        设置token自动刷新
        //tokenServices.setSupportRefreshToken(true);
        储存token
        tokenServices.setTokenStore(tokenStore());
        设置token过期时间为12小时(适用于token储存到内存模式)
        //tokenServices.setAccessTokenValiditySeconds(43200);
        //tokenServices.setRefreshTokenValiditySeconds(72000);
        return tokenServices;
    }

}

注意:测试前要新建两张表

1.储存客户端详情表:配置客户端的信息
CREATE TABLE oauth_client_details (
client_id varchar(48) NOT NULL,
resource_ids varchar(256) DEFAULT NULL,
client_secret varchar(256) DEFAULT NULL,
scope varchar(256) DEFAULT NULL,
authorized_grant_types varchar(256) DEFAULT NULL,
web_server_redirect_uri varchar(256) DEFAULT NULL,
authorities varchar(256) DEFAULT NULL,
access_token_validity int(11) DEFAULT NULL,
refresh_token_validity int(11) DEFAULT NULL,
additional_information varchar(4096) DEFAULT NULL,
autoapprove varchar(256) DEFAULT NULL,
PRIMARY KEY (client_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.储存token信息表:
CREATE TABLE oauth_access_token (
token_id varchar(255) DEFAULT NULL,
token blob,
authentication_id varchar(255) DEFAULT NULL,
user_name varchar(255) DEFAULT NULL,
client_id varchar(255) DEFAULT NULL,
authentication blob,
refresh_token varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

三、配置类继承WebSecurityConfigurerAdapter

/**
 * 资源安全配置
 * @author ry
 * @date 2021/11/17 14:31
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

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

三、配置类继承ResourceServerConfigurerAdapter

/**
 * 认证授权配置
 *
 * @author ry
 * @date 2021/11/18 9:32
 */
@Configuration
@EnableResourceServer
public class Oauth2ResourceServer extends ResourceServerConfigurerAdapter {

    private static final String TEST= "test";

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        // 指定资源可以访问,有多个资源可以继续添加,对应表里的字段值,表里多个字段值用逗号隔开
        resources.resourceId(TEST).stateless(true);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
        http.authorizeRequests()
                //指定不同请求方式访问资源所需要的权限。
                .antMatchers(HttpMethod.POST, "/test1").access("#oauth2.hasScope('test1')")
                .antMatchers(HttpMethod.POST, "/test2").access("#oauth2.hasScope('test2')")
                .and()
                .headers().addHeaderWriter((request, response) -> {
            response.addHeader("Access-Control-Allow-Origin", "*");//允许跨域
            if (request.getMethod().equals("OPTIONS")) {//如果是跨域的预检请求,则原封不动向下传达请求头信息
                response.setHeader("Access-Control-Allow-Methods", request.getHeader("Access-Control-Request-Method"));
                response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
            }
        });
    }
}

四、postman测试

在这里插入图片描述

五、获取到token后,请求头携带token访问指定的资源即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 中使用 OAuth 2.0 的客户端模式,可以通过集成 Spring Security OAuth2 来实现。下面是一个简单的配置示例: 首先,在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> ``` 然后,在 Spring Boot 的配置文件(application.properties 或 application.yml)中添加以下配置: ```properties spring.security.oauth2.client.registration.my-client.client-id=your-client-id spring.security.oauth2.client.registration.my-client.client-secret=your-client-secret spring.security.oauth2.client.registration.my-client.authorization-grant-type=client_credentials spring.security.oauth2.client.provider.my-client.token-uri=your-token-uri ``` 上述配置中,`my-client` 是一个自定义的客户端标识,你可以根据实际情况进行修改。`your-client-id` 和 `your-client-secret` 是你在授权服务器注册的客户端ID和秘钥。`your-token-uri` 是授权服务器的令牌请求地址。 最后,创建一个配置类来启用 Spring Security OAuth2 客户端模式: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .oauth2Login(); } } ``` 这个配置类继承自 `WebSecurityConfigurerAdapter`,并覆写了 `configure` 方法来配置请求的授权规则。上述示例中的配置要求所有请求都需要进行认证。 以上就是使用 Spring Security 实现 OAuth 2.0 客户端模式的简单配置示例。你可以根据具体需求进行更详细的配置和调整。 希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值