parent
4dea290b68
commit
4c51872e4e
@ -0,0 +1,72 @@
|
||||
package com.yami.shop.api.controller;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.yami.shop.api.security.AuthenticationToken;
|
||||
import com.yami.shop.bean.model.User;
|
||||
import com.yami.shop.bean.param.UserRegisterParam;
|
||||
import com.yami.shop.common.exception.YamiShopBindException;
|
||||
import com.yami.shop.common.util.IPHelper;
|
||||
import com.yami.shop.security.handler.LoginAuthSuccessHandler;
|
||||
import com.yami.shop.security.model.AppConnect;
|
||||
import com.yami.shop.security.service.YamiUserDetailsService;
|
||||
import com.yami.shop.service.UserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 用户信息
|
||||
*
|
||||
* @author SJL
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@Api(tags = "用户注册相关接口")
|
||||
@AllArgsConstructor
|
||||
public class UserRegisterController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@PostMapping("/register")
|
||||
@ApiOperation(value = "注册", notes = "用户注册或绑定手机号接口")
|
||||
public ResponseEntity<Boolean> register(@Valid @RequestBody UserRegisterParam userRegisterParam) {
|
||||
userRegisterParam.setPassword(passwordEncoder.encode(userRegisterParam.getPassword()));
|
||||
return ResponseEntity.ok(userService.insertUser(userRegisterParam));
|
||||
}
|
||||
|
||||
|
||||
@PutMapping("/updatePwd")
|
||||
@ApiOperation(value = "修改密码", notes = "修改密码")
|
||||
public ResponseEntity<Void> updatePwd(@Valid @RequestBody UserRegisterParam userRegisterParam) {
|
||||
User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserMobile, userRegisterParam.getUserMail()));
|
||||
if (user == null) {
|
||||
// 无法获取用户信息
|
||||
throw new YamiShopBindException("yami.user.no.exist");
|
||||
}
|
||||
if (StrUtil.isBlank(userRegisterParam.getPassword())) {
|
||||
// 新密码不能为空
|
||||
throw new YamiShopBindException("yami.user.password.no.exist");
|
||||
}
|
||||
if (StrUtil.equals(passwordEncoder.encode(userRegisterParam.getPassword()), user.getLoginPassword())) {
|
||||
// 新密码不能与原密码相同
|
||||
throw new YamiShopBindException("yami.user.password.check");
|
||||
}
|
||||
user.setModifyTime(new Date());
|
||||
user.setLoginPassword(passwordEncoder.encode(userRegisterParam.getPassword()));
|
||||
userService.updateById(user);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,154 @@
|
||||
package com.yami.shop.api.security;
|
||||
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import com.yami.shop.common.exception.YamiShopBindException;
|
||||
import com.yami.shop.common.util.Json;
|
||||
import com.yami.shop.common.xss.XssUtil;
|
||||
import com.yami.shop.security.exception.BadCredentialsException;
|
||||
import com.yami.shop.security.exception.UsernameNotFoundException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author SJL
|
||||
*/
|
||||
public class YamiAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {
|
||||
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
private PasswordEncoder passwordEncoder;
|
||||
/**
|
||||
* 请求字符串的最大长度 1m
|
||||
*/
|
||||
public static final int MAX_STRING_SIZE = 1024 * 1024;
|
||||
|
||||
protected YamiAuthenticationProcessingFilter(String defaultFilterProcessesUrl) {
|
||||
super(defaultFilterProcessesUrl);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setUserDetailsService(UserDetailsService userDetailsService) {
|
||||
this.userDetailsService = userDetailsService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Autowired
|
||||
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
|
||||
super.setAuthenticationManager(authenticationManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Autowired
|
||||
public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
|
||||
super.setAuthenticationSuccessHandler(successHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Autowired
|
||||
public void setAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler) {
|
||||
super.setAuthenticationFailureHandler(failureHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, ServletException{
|
||||
if (!ServletUtil.METHOD_POST.equals(request.getMethod())) {
|
||||
throw new HttpRequestMethodNotSupportedException(request.getMethod(), new String[] { "POST" });
|
||||
}
|
||||
|
||||
AuthenticationToken authenticationToken = Json.parseObject(getStringFromStream(request), AuthenticationToken.class);
|
||||
UserDetails userDetails = getUserDetails(authenticationToken);
|
||||
return handleAuthenticationToken(authenticationToken,userDetails);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
* @param authenticationToken
|
||||
* @return
|
||||
*/
|
||||
protected UserDetails getUserDetails(AuthenticationToken authenticationToken) {
|
||||
UserDetails user;
|
||||
try {
|
||||
user = userDetailsService.loadUserByUsername(authenticationToken.getPrincipal());
|
||||
} catch (UsernameNotFoundException var6) {
|
||||
// 账号或密码不正确
|
||||
throw new UsernameNotFoundException("账号或密码不正确");
|
||||
}
|
||||
if (!user.isEnabled()) {
|
||||
// 账号已被锁定,请联系管理员
|
||||
throw new UsernameNotFoundException("账号已被锁定,请联系管理员");
|
||||
}
|
||||
|
||||
String encodedPassword = user.getPassword();
|
||||
String rawPassword = authenticationToken.getCredentials().toString();
|
||||
|
||||
// 密码不正确
|
||||
if (!passwordEncoder.matches(rawPassword,encodedPassword)){
|
||||
// 账号或密码不正确
|
||||
throw new BadCredentialsException("账号或密码不正确");
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 保存用户信息
|
||||
*/
|
||||
protected AuthenticationToken handleAuthenticationToken(AuthenticationToken authentication, UserDetails userDetails) {
|
||||
// 保存用户信息
|
||||
authentication.setPrincipal(userDetails.getUsername());
|
||||
authentication.setDetails(userDetails);
|
||||
authentication.setAuthenticated(true);
|
||||
return authentication;
|
||||
}
|
||||
|
||||
|
||||
public String getStringFromStream(HttpServletRequest req) {
|
||||
if (req.getContentLength() > MAX_STRING_SIZE) {
|
||||
// 请求数据过长
|
||||
throw new YamiShopBindException("yami.request.data.too.long");
|
||||
}
|
||||
ServletInputStream is;
|
||||
try {
|
||||
is = req.getInputStream();
|
||||
int nRead = 1;
|
||||
int nTotalRead = 0;
|
||||
byte[] bytes = new byte[1024];
|
||||
while (nRead > 0) {
|
||||
nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);
|
||||
if (nRead > 0) {
|
||||
nTotalRead = nTotalRead + nRead;
|
||||
}
|
||||
}
|
||||
if (nTotalRead > MAX_STRING_SIZE) {
|
||||
// 请求数据过长
|
||||
throw new YamiShopBindException("yami.request.data.too.long");
|
||||
}
|
||||
return XssUtil.clean(new String(bytes, 0, nTotalRead, StandardCharsets.UTF_8));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue