Helius
2020-05-13 895dedb7341da20fb815010630a99988005b81a1
add userauth
1 files added
5 files modified
62 ■■■■■ changed files
src/main/java/com/xcong/excoin/common/annotations/UserAuth.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/common/system/controller/LoginController.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/common/system/dto/LoginDto.java 8 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/configurations/WebMvcConfig.java 7 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/configurations/security/UserAuthenticationArgumentResolver.java 39 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/test/controller/TestUserController.java 5 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/common/annotations/UserAuth.java
@@ -6,6 +6,7 @@
/**
 * 自动注入当前登录用户
 *
 * @author wzy
 */
@Target({ ElementType.PARAMETER, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
src/main/java/com/xcong/excoin/common/system/controller/LoginController.java
@@ -7,6 +7,7 @@
import com.xcong.excoin.common.system.bean.LoginUserBean;
import com.xcong.excoin.common.system.dto.LoginDto;
import com.xcong.excoin.utils.RedisUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -26,6 +27,7 @@
 * @Version V1.0
 **/
@Slf4j
@Api(value = "登陆类", tags = "登陆类")
@RestController
@RequestMapping(value = "/")
public class LoginController {
src/main/java/com/xcong/excoin/common/system/dto/LoginDto.java
@@ -1,5 +1,7 @@
package com.xcong.excoin.common.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@@ -9,13 +11,15 @@
 * @date 2020-05-12
 **/
@Data
@ApiModel(value = "登陆接口接收类", description = "登陆接口接收类")
public class LoginDto {
    @ApiModelProperty(value = "用户名", example = "123")
    @NotBlank(message = "用户名或密码错误")
    private String username;
    @ApiModelProperty(value = "密码", example = "3333")
    @NotBlank(message = "用户名或密码错误")
    private String password;
    private String code;
}
src/main/java/com/xcong/excoin/configurations/WebMvcConfig.java
@@ -1,12 +1,16 @@
package com.xcong.excoin.configurations;
import com.xcong.excoin.configurations.security.UserAuthenticationArgumentResolver;
import com.xcong.excoin.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
/**
 * @author wzy
@@ -17,7 +21,8 @@
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new UserAuthenticationArgumentResolver());
    }
    /**
src/main/java/com/xcong/excoin/configurations/security/UserAuthenticationArgumentResolver.java
New file
@@ -0,0 +1,39 @@
package com.xcong.excoin.configurations.security;
import com.xcong.excoin.common.annotations.UserAuth;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import org.springframework.core.MethodParameter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
/**
 * 自动注入登陆用户
 *
 * @author wzy
 * @date 2020-05-13
 **/
public class UserAuthenticationArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterAnnotation(UserAuth.class) != null && parameter.getParameterType().equals(MemberEntity.class);
    }
    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }
        MemberEntity auth = null;
        if (authentication.getPrincipal() != null) {
            auth = (MemberEntity) authentication.getPrincipal();
        }
        return auth;
    }
}
src/main/java/com/xcong/excoin/modules/test/controller/TestUserController.java
@@ -2,7 +2,9 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcong.excoin.common.annotations.UserAuth;
import com.xcong.excoin.common.response.Result;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.test.dto.TestUserDto;
import com.xcong.excoin.modules.test.entity.TestUserEntity;
import com.xcong.excoin.modules.test.mapper.TestUserEntityMapper;
@@ -38,8 +40,9 @@
    @PostMapping(value = "/findUserInPage")
    public Result findUserInPage(@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                 @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                 @RequestBody @Valid TestUserDto testUserDto) {
                                 @RequestBody @Valid TestUserDto testUserDto, @UserAuth MemberEntity memberEntity) {
        log.info("--->{}",SecurityContextHolder.getContext().getAuthentication());
        log.info("----->{}", memberEntity);
        Page<TestUserEntity> page = new Page<>(pageNum, pageSize);
        log.info(testUserDto.getName());
        IPage<TestUserEntity> list = testUserService.page(page);