Java自定义登录实现

前言

Java现在已经是Spring的天下了。本文不用Security自定义实现简单登录。

基于AOP

maven依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

AuthorizeAspect.java

  • 除了login接口其他均需要认证
  • unauth接口黑名单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.idcmind.antslogin01.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
* @author :当我遇上你
* @email :idea360@foxmail.com
* @公众号 当我遇上你
*/
@Aspect
@Component
@Slf4j
public class AuthorizeAspect {

@Pointcut(
"execution(public * com.idcmind.antslogin01.controller.*.*(..))"
+"&& !execution(public * com.idcmind.antslogin01.controller.BController.login(..))"
)
public void verify() {}

@Before("verify()")
public void doBefore(JoinPoint joinPoint){

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();

// 记录下请求内容
log.info("URL : " + request.getRequestURL().toString());
log.info("HTTP_METHOD : " + request.getMethod());
log.info("CLASS_NAME : " + joinPoint.getSignature().getDeclaringTypeName());
log.info("METHOD_NAME : " + joinPoint.getSignature().getName());
log.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));

if ("unauth".equalsIgnoreCase(joinPoint.getSignature().getName())) {
throw new RuntimeException("模拟未通过认证");
}
}
}

基于拦截器

AuthorizationInterceptor.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.idcmind.antslogin02.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author :当我遇上你
* @email :idea360@foxmail.com
* @公众号 当我遇上你
*/
@Component
public class AuthorizationInterceptor extends HandlerInterceptorAdapter {


public static final String USER_KEY = "userId";

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

String requestURI = request.getRequestURI();
if (requestURI.equalsIgnoreCase("/test1")) {
return true;
}
if (requestURI.equalsIgnoreCase("/test2")) {
throw new RuntimeException("模拟认证失败");
}

return true;
}
}

配置拦截器

WebMvcConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.idcmind.antslogin02.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* @author :当我遇上你
* @email :idea360@foxmail.com
* @公众号 当我遇上你
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private AuthorizationInterceptor authorizationInterceptor;


@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**");
}

}

最后

本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。