文章目录
一. 创建模块
- 一. 创建模块
- 二. 添加依赖
- 三.编写yml配置文件
- 四.创建启动类
- 五. 配置Jwt的拦截器
在tensequare_parent工程中,创建子模块tensquare_friend
二. 添加依赖在tensquare_friend 中,添加依赖.内容如下
org.springframework.boot
spring-boot-starter-data-jpa
com.tensquare
tensquare_common
1.0-SNAPSHOT
mysql
mysql-connector-java
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
三.编写yml配置文件
在resources包下,创建application.yml配置文件,内容如下
server:
port: 9010
spring:
application:
name: tensquare-friend #指定服务名
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://16:3306/tensquare_friend?characterEncoding=UTF8
username: root
password:
jpa:
database: MySQL
show-sql: true
jwt:
config:
key: itcast # 设置token的盐为 itcast
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/ #此处为eureka的服务端地址
instance:
prefer-ip-address: true # 此处配置项的含义是,部署线上时,模块之间可以跨域访问
四.创建启动类
在com.tensquare.friend包下,创建启动类 内容如下. 注意加上如下的四个注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
分别代表是SpringBoot的启动类,注册Eureka的客户端,使用Feign去发现服务,开启服务的发现.
package com.tensquare.friend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import util.JwtUtil;
/**
* 类名称:FriendApplication
* 类描述:启动类
*
* @author: taohongchao
* 创建时间:2019/2/17 14:41
* Version 1.0
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
public class FriendApplication {
public static void main(String[] args){
SpringApplication.run(FriendApplication.class);
}
/**
* 把jwt对象注入容器中
* @return
*/
@Bean
public JwtUtil jwtUtil(){
return new JwtUtil();
}
}
五. 配置Jwt的拦截器
复制JwtInterceptor 和InterceptorConfig 到项目中,内容如下
package com.tensquare.friend.interceptor;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import util.JwtUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 类名称:JwtInterceptor
* 类描述:jwt的全局拦截器
*
* @author: taohongchao
* 创建时间:2019/2/16 13:56
* Version 1.0
*/
@Component
public class JwtInterceptor implements HandlerInterceptor {
@Autowired
private JwtUtil jwtUtil;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("经过了拦截器");
String header = request.getHeader("Authorization");
if (header != null && !"".equals(header)) {
//header信息不为空, 那么就进行解析的操作
if (header.startsWith("Bearer ")) {
//得到token
String token = header.substring(7);
//对令牌进行验证
//解析token的过程可能会抛出异常,因此使用try catch
try {
//解析token
Claims claims = jwtUtil.parseJWT(token);
//获取角色信息
String roles = (String) claims.get("roles");
//判断角色是否为空,或者是不是admin角色
if (roles != null && "admin".equals(roles)) {
//如果角色不为空,并且是admin角色. 那么把token的信息,放入request域中,键为claims_admin, 值为token
request.setAttribute("claims_admin",token);
}
if (roles != null && "user".equals(roles)) {
//如果角色不为空,并且是user角色. 那么把token的信息,放入request域中,键为claims_user, 值为token
request.setAttribute("claims_user",token);
}
} catch (RuntimeException e) {
//解析token的过程抛出异常, 也代表权限不足
//异常可能的原因是token过期等等
throw new RuntimeException("令牌不正确!");
}
}
}
//return true代表放行, return false 代表不放行
//任何情况下都要放行, 最终具体的操作,要看其业务
//拦截器只负责把请求头中包含token信息进行解析
return true;
}
}
package com.tensquare.friend.config;
import com.tensquare.friend.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* 类名称:InterceptorConfig
* 类描述: 拦截器的配置类
*
* @author: taohongchao
* 创建时间:2019/2/16 14:00
* Version 1.0
*/
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {
@Autowired
private JwtInterceptor jwtInterceptor;
/**
* 方法名: addInterceptors
* 方法描述: 添加拦截器的规则
* 修改日期: 2019/2/16 14:02
* @param registry
* @return void
* @author taohongchao
* @throws
*/
@Override
protected void addInterceptors(InterceptorRegistry registry) {
//添加jwt的拦截器
registry.addInterceptor(jwtInterceptor)
//拦截所有的请求
.addPathPatterns("/**")
//放行登录的请求
.excludePathPatterns("/**/login/**");
}
}