|  |  |  | 
|---|
|  |  |  | package com.xcong.excoin.common.system.controller; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import cn.hutool.core.codec.Base64; | 
|---|
|  |  |  | import cn.hutool.core.util.IdUtil; | 
|---|
|  |  |  | import cn.hutool.crypto.SecureUtil; | 
|---|
|  |  |  | import cn.hutool.crypto.asymmetric.KeyType; | 
|---|
|  |  |  | import cn.hutool.crypto.asymmetric.RSA; | 
|---|
|  |  |  | import cn.hutool.crypto.asymmetric.Sign; | 
|---|
|  |  |  | import cn.hutool.crypto.asymmetric.SignAlgorithm; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.xcong.excoin.common.contants.AppContants; | 
|---|
|  |  |  | import com.xcong.excoin.common.response.Result; | 
|---|
|  |  |  | import com.xcong.excoin.common.system.bean.LoginUserBean; | 
|---|
|  |  |  | import com.xcong.excoin.common.system.dto.LoginDto; | 
|---|
|  |  |  | import com.xcong.excoin.common.system.dto.RegisterDto; | 
|---|
|  |  |  | import com.xcong.excoin.configurations.properties.ApplicationProperties; | 
|---|
|  |  |  | import com.xcong.excoin.configurations.properties.SecurityProperties; | 
|---|
|  |  |  | import com.xcong.excoin.modules.member.service.MemberService; | 
|---|
|  |  |  | import com.xcong.excoin.utils.RedisUtils; | 
|---|
|  |  |  | import io.swagger.annotations.Api; | 
|---|
|  |  |  | import io.swagger.annotations.ApiOperation; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Value; | 
|---|
|  |  |  | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @Version V1.0 | 
|---|
|  |  |  | **/ | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Api(value = "登陆注册类", tags = "登陆注册类") | 
|---|
|  |  |  | @RestController | 
|---|
|  |  |  | @RequestMapping(value = "/") | 
|---|
|  |  |  | public class LoginController { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${rsa.private_key}") | 
|---|
|  |  |  | private String privateKey; | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private MemberService memberservice; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private ApplicationProperties applicationProperties; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private SecurityProperties securityProperties; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private AuthenticationManagerBuilder authenticationManagerBuilder; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private RedisUtils redisUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "登陆接口", notes = "登陆接口") | 
|---|
|  |  |  | @PostMapping("/login") | 
|---|
|  |  |  | public Result login(@RequestBody @Validated LoginDto loginDto) { | 
|---|
|  |  |  | UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(loginDto.getUsername(), loginDto.getPassword()); | 
|---|
|  |  |  | // 将账号密码交给spring security验证,并调用userServiceDetails | 
|---|
|  |  |  | UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(loginDto.getUsername(), SecureUtil.md5(loginDto.getPassword())); | 
|---|
|  |  |  | Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authToken); | 
|---|
|  |  |  | String token = IdUtil.simpleUUID(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 获取当前验证过后的用户 | 
|---|
|  |  |  | LoginUserBean loginUserBean = (LoginUserBean) authentication.getPrincipal(); | 
|---|
|  |  |  | redisUtils.set(AppContants.APP_LOGIN_PREFIX + token, JSONObject.toJSONString(loginUserBean), 300000); | 
|---|
|  |  |  | Map<String, Object> authInfo = new HashMap<String, Object>(2){ | 
|---|
|  |  |  | { | 
|---|
|  |  |  | put("token", token); | 
|---|
|  |  |  | put("user", loginUserBean); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 生成UUID作为token | 
|---|
|  |  |  | String token = IdUtil.simpleUUID(); | 
|---|
|  |  |  | redisUtils.set(AppContants.APP_LOGIN_PREFIX + token, JSONObject.toJSONString(loginUserBean), applicationProperties.getRedisExpire()); | 
|---|
|  |  |  | Map<String, Object> authInfo = new HashMap<>(); | 
|---|
|  |  |  | // 开启debug模式,则将加密后的token返回 | 
|---|
|  |  |  | if (applicationProperties.isDebug()) { | 
|---|
|  |  |  | authInfo.put("token", token); | 
|---|
|  |  |  | authInfo.put("rsaToken", generateAsaToken(token)); | 
|---|
|  |  |  | authInfo.put("user", loginUserBean); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | authInfo.put("token", token); | 
|---|
|  |  |  | authInfo.put("user", loginUserBean); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return Result.ok("success", authInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public String generateAsaToken(String token) { | 
|---|
|  |  |  | RSA rsa = new RSA(null, securityProperties.getPublicKey()); | 
|---|
|  |  |  | return rsa.encryptBase64(token + "_" +System.currentTimeMillis(), KeyType.PublicKey); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "app注册接口", notes = "app注册接口,验证码必须输入可默认为123456") | 
|---|
|  |  |  | @PostMapping(value = "/register") | 
|---|
|  |  |  | public Result register(@RequestBody @Validated RegisterDto registerDto) { | 
|---|
|  |  |  | return memberservice.register(registerDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|