token发布与验证(服务端)

令牌处理器配置

创建JWT令牌配置类,基于这个类实现令牌对象的创建和解析

JWT令牌的构成有三部分构成:

  • 1)HEADER (头部信息:令牌类型,签名算法)
  • 2)PAYLOAD (数据信息-用户信息,权限信息,令牌失效时间,...)
  • 3)SIGNATURE (签名信息-对header和payload部分进行加密签名)
package com.jt.resource.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * 创建JWT令牌配置类,基于这个类实现令牌对象的创建和解析.
 * JWT令牌的构成有三部分构成:
 * 1)HEADER (头部信息:令牌类型,签名算法)
 * 2)PAYLOAD (数据信息-用户信息,权限信息,令牌失效时间,...)
 * 3)SIGNATURE (签名信息-对header和payload部分进行加密签名)
 */
@Configuration
public class TokenConfig {
    /**定义令牌签发口令(暗号),这个口令自己定义即可
    *在对header和PAYLOAD部分进行签名时,需要的一个口令
    * 1)生成的令牌需要这个密钥进行签名
    * 2)获取的令牌需要使用这个密钥进行验签(校验令牌合法性,是否被篡改过)
    */
    private static final String SIGNING_KEY= "sayhi";
    //初始化令牌生成策略(默认生成策略 UUID)
    //这里我们采用JWT方式生成令牌
    @Bean
    public TokenStore tokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }
    //JWT令牌构成:header(签名算法,令牌类型),payload(数据部分),Signing(签名)
    //这里的签名可以简单理解为加密,加密时会使用header中算法以及我们自己提供的密钥,
    //这里加密的目的是为了防止令牌被篡改。(这里密钥要保管好,要存储在服务端)
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter jwtAccessTokenConverter=new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey(SIGNING_KEY);//设置密钥
        return jwtAccessTokenConverter;
    }
}

 将所有的认证和授权配进行整合

package com.jt.resource.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * 思考?对于一个系统而言,它资源的访问权限你是如何进行分类设计的
 * 1)不需要登录就可以访问(例如12306查票)
 * 2)登录以后才能访问(例如12306的购票)
 * 3)登录以后没有权限也不能访问(例如会员等级不够不让执行一些相关操作)
 */
@Configuration
@EnableResourceServer
//启动方法上的权限控制,需要授权才可访问的方法上添加@PreAuthorize等相关注解
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        //1.关闭跨域攻击
        http.csrf().disable();
        //2.放行相关请求
        http.authorizeRequests()
                .antMatchers("/resource/upload/**")
                .authenticated()
                .anyRequest().permitAll();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值