单点登录
1.父工程jar依赖
<jwt.version>0.7.0</jwt.version>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>
2.配置yml文件 application.yml
server:
port: 8150
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/aaa?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 123456
application:
name: ucenter-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
redis:
host: 192.168.211.100
port: 6379
mybatis-plus:
mapper-locations: classpath:com/lee/ucenterservice/mapper/xml/*.xml
logging:
level:
com.lee.ucenterservice.mapper: debug
wx:
open:
appid: wxed9954c01bb89b47
appsecret: a7482517235173ddb4083788de60b90e
redirecturl: http://guli.shop/api/ucenter/wx/callback
3.JwtUtils —>工具类—>生成token令牌
package com.lee.util;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
public class JwtUtils {
public static final long EXPIRE = 1000 * 60 * 60 * 24;
public static final String APP_SECRET = "ukc8BDbRigUDaY6pZFfWus2jZWLPHO";
public static String getJwtToken(String id, String nickname) {
String JwtToken = Jwts.builder()
.setHeaderParam("typ", "JWT")
.setHeaderParam("alg", "HS256")
.setSubject("AAA-user")
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
.claim("id", id)
.claim("nickname", nickname)
.signWith(SignatureAlgorithm.HS256, APP_SECRET)
.compact();
return JwtToken;
}
public static boolean checkToken(String jwtToken) {
if (StringUtils.isEmpty(jwtToken)) {
return false;
}
try {
Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean checkToken(HttpServletRequest request) {
try {
String jwtToken = request.getHeader("token");
if (StringUtils.isEmpty(jwtToken)){
return false;
}
Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static String getMemberIdByJwtToken(HttpServletRequest request) {
String jwtToken = request.getHeader("token");
if (StringUtils.isEmpty(jwtToken)){
return "";
}
Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
Claims claims = claimsJws.getBody();
return (String) claims.get("id");
}
}
4.实体Vo—>LoginVo—>UcenterVo
@Data
public class LoginVo {
@ApiModelProperty(value = "手机号")
private String mobile;
@ApiModelProperty(value = "密码")
private String password;
}
@Data
public class UcenterVo {
@ApiModelProperty(value = "手机号")
private String mobile;
@ApiModelProperty(value = "密码")
private String password;
@ApiModelProperty(value = "昵称")
private String nickname;
@ApiModelProperty(value = "验证码")
private String code;
}
4.UcenterMemberController ---->登录,并生成token令牌—>获取token中的id----根据id查询用户的详细信息
@RestController
@RequestMapping("/ucenterservice/member")
@Api(tags="登录与注册管理接口")
public class UcenterMemberController {
@Autowired
private UcenterMemberService ucenterMemberService;
@ApiOperation(value = "账号密码登录")
@PostMapping("login")
public R login(@RequestBody LoginVo loginVo){
String token=ucenterMemberService.login(loginVo);
return R.ok().data("token",token);
}
@ApiOperation(value = "根据token获取用户的详细信息")
@GetMapping("getUserInfoDetail")
public R getUserInfoDetail(HttpServletRequest request){
String id = JwtUtils.getMemberIdByJwtToken(request);
UcenterMember member = this.ucenterMemberService.getById(id);
return R.ok().data("member",member);
}
}
5.service–>UcenterMemberService
public interface UcenterMemberService extends IService<UcenterMember> {
String login(LoginVo loginVo);
}
6.service—>impl—>UcenterMemberServiceImpl
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
@Override
public String login(LoginVo loginVo) {
String mobile = loginVo.getMobile();
String password=loginVo.getPassword();
if(StringUtils.isEmpty(mobile)|| StringUtils.isEmpty(password)){
throw new AAAException(20001,"账户或密码不能为空");
}
UcenterMember ucenterMember2 = this.getOne(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
if(ucenterMember2==null){
throw new AAAException(20001,"账户不存在");
}
if(!StringUtils.equals(MD5.encrypt(password),ucenterMember2.getPassword())){
throw new AAAException(20001,"密码错误");
}
if(ucenterMember2.getIsDisabled()){
throw new AAAException(20001,"该账户被冻结");
}
String token = JwtUtils.getJwtToken(ucenterMember2.getId(), ucenterMember2.getNickname());
return token;
}
}