日志记录
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package me.zhengjie.config;
|
||||
package cn.ysk.cashier.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.*;
|
||||
@@ -12,7 +12,7 @@ import java.util.Collections;
|
||||
|
||||
|
||||
@Configuration
|
||||
@MapperScan("me.zhengjie.mybatis.mapper")
|
||||
@MapperScan("cn.ysk.cashier.mybatis.mapper")
|
||||
@EnableTransactionManagement
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
|
||||
@@ -18,6 +18,8 @@ package cn.ysk.cashier.config.security.rest;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.ysk.cashier.config.security.config.bean.LoginCodeEnum;
|
||||
import cn.ysk.cashier.config.security.security.TokenProvider;
|
||||
import cn.ysk.cashier.pojo.shop.TbPlussShopStaff;
|
||||
import cn.ysk.cashier.repository.shop.TbPlussShopStaffRepository;
|
||||
import cn.ysk.cashier.utils.*;
|
||||
import com.wf.captcha.base.Captcha;
|
||||
import io.swagger.annotations.Api;
|
||||
@@ -40,16 +42,19 @@ import cn.ysk.cashier.pojo.shop.TbShopInfo;
|
||||
import cn.ysk.cashier.repository.shop.TbShopInfoRepository;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -69,10 +74,10 @@ public class AuthorizationController {
|
||||
private final TokenProvider tokenProvider;
|
||||
private final AuthenticationManagerBuilder authenticationManagerBuilder;
|
||||
private final TbShopInfoRepository tbShopInfoRepository;
|
||||
private final TbPlussShopStaffRepository staffRepository;
|
||||
@Resource
|
||||
private LoginProperties loginProperties;
|
||||
|
||||
@Log("用户登录")
|
||||
@ApiOperation("登录授权")
|
||||
@AnonymousPostMapping(value = "/login")
|
||||
public ResponseEntity<Object> login(@Validated @RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {
|
||||
@@ -97,17 +102,22 @@ public class AuthorizationController {
|
||||
// UserDetails userDetails = userDetailsService.loadUserByUsername(userInfo.getUsername());
|
||||
// Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
|
||||
// SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
String token = tokenProvider.createToken(authentication);
|
||||
final JwtUserDto jwtUserDto = (JwtUserDto) authentication.getPrincipal();
|
||||
// 返回 token 与 用户信息
|
||||
TbShopInfo byAccount = tbShopInfoRepository.findByAccount(jwtUserDto.getUsername());
|
||||
// TbShopInfo byAccount = tbShopInfoRepository.findByAccount(jwtUserDto.getUsername());
|
||||
TbPlussShopStaff tbPlussShopStaff = staffRepository.queryByAccount(jwtUserDto.getUsername());
|
||||
if (tbPlussShopStaff != null && tbPlussShopStaff.getType().equals("staff")) {
|
||||
Integer isManage = tbPlussShopStaff.getIsManage();
|
||||
if (isManage != null && isManage != 1) {
|
||||
throw new BadRequestException("该账号无权限登录,请联系管理员");
|
||||
}
|
||||
}
|
||||
TbShopInfo byAccount = tbShopInfoRepository.findById(Integer.valueOf(tbPlussShopStaff.getShopId())).get();
|
||||
//校验商户商户激活是否到期(未激活)
|
||||
|
||||
|
||||
String token = tokenProvider.createToken(authentication, tbPlussShopStaff.getShopId());
|
||||
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
put("user", jwtUserDto);
|
||||
if (byAccount!= null){
|
||||
if (byAccount != null) {
|
||||
put("shopId", byAccount.getId());
|
||||
put("shopName", byAccount.getShopName());
|
||||
put("logo", byAccount.getLogo());
|
||||
@@ -115,7 +125,7 @@ public class AuthorizationController {
|
||||
|
||||
}};
|
||||
// 保存在线信息
|
||||
onlineUserService.save(jwtUserDto, token, request,byAccount.getId());
|
||||
onlineUserService.save(jwtUserDto, token, request, byAccount.getId());
|
||||
|
||||
if (loginProperties.isSingleLogin()) {
|
||||
//踢掉之前已经登录的token
|
||||
@@ -126,13 +136,14 @@ public class AuthorizationController {
|
||||
|
||||
/**
|
||||
* 小程序登录
|
||||
*
|
||||
* @param authUser
|
||||
* @param request
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@PostMapping(value = "/appletsLogin")
|
||||
public ResponseEntity<Object> appletsLogin(@RequestBody AuthUserDto authUser,HttpServletRequest request)throws Exception{
|
||||
public ResponseEntity<Object> appletsLogin(@RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {
|
||||
// 密码解密
|
||||
String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
|
||||
|
||||
@@ -140,18 +151,18 @@ public class AuthorizationController {
|
||||
new UsernamePasswordAuthenticationToken(authUser.getUsername(), password);
|
||||
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
// 生成令牌与第三方系统获取令牌方式
|
||||
String token = tokenProvider.createToken(authentication);
|
||||
final JwtUserDto jwtUserDto = (JwtUserDto) authentication.getPrincipal();
|
||||
// 保存在线信息
|
||||
onlineUserService.save(jwtUserDto, token,request,null);
|
||||
// 返回 token 与 用户信息
|
||||
TbShopInfo byAccount = tbShopInfoRepository.findByAccount(jwtUserDto.getUsername());
|
||||
// 生成令牌与第三方系统获取令牌方式
|
||||
String token = tokenProvider.createToken(authentication, byAccount.getId().toString());
|
||||
// 保存在线信息
|
||||
onlineUserService.save(jwtUserDto, token, request, null);
|
||||
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
put("user", jwtUserDto);
|
||||
if (byAccount!= null){
|
||||
put("shopId",byAccount.getId());
|
||||
if (byAccount != null) {
|
||||
put("shopId", byAccount.getId());
|
||||
}
|
||||
|
||||
}};
|
||||
|
||||
@@ -18,6 +18,8 @@ package cn.ysk.cashier.config.security.security;
|
||||
import cn.hutool.core.date.DateField;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.ysk.cashier.utils.SpringContextHolder;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.jsonwebtoken.*;
|
||||
import io.jsonwebtoken.io.Decoders;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
@@ -47,6 +49,11 @@ public class TokenProvider implements InitializingBean {
|
||||
private JwtParser jwtParser;
|
||||
private JwtBuilder jwtBuilder;
|
||||
|
||||
/**
|
||||
* token秘钥
|
||||
*/
|
||||
private static final String TOKEN_SECRET = "BBDFSDFHFGHSGSRTRESDFSDFS";
|
||||
|
||||
public TokenProvider(SecurityProperties properties, RedisUtils redisUtils) {
|
||||
this.properties = properties;
|
||||
this.redisUtils = redisUtils;
|
||||
@@ -70,11 +77,12 @@ public class TokenProvider implements InitializingBean {
|
||||
* @param authentication /
|
||||
* @return /
|
||||
*/
|
||||
public String createToken(Authentication authentication) {
|
||||
public String createToken(Authentication authentication,String shopId) {
|
||||
return jwtBuilder
|
||||
// 加入ID确保生成的 Token 都不一致
|
||||
.setId(IdUtil.simpleUUID())
|
||||
.claim(AUTHORITIES_KEY, authentication.getName())
|
||||
.claim("shopId",shopId)
|
||||
.setSubject(authentication.getName())
|
||||
.compact();
|
||||
}
|
||||
@@ -120,4 +128,13 @@ public class TokenProvider implements InitializingBean {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getShopId() {
|
||||
HttpServletRequest request = SpringContextHolder.getRequest();
|
||||
final String requestHeader = request.getHeader(properties.getHeader());
|
||||
if (requestHeader != null && requestHeader.startsWith(properties.getTokenStartWith())) {
|
||||
return getClaims(requestHeader.substring(7)).get("shopId").toString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user