learnjava

Dec 16, 2021

这里记录一写java的学习点,知识点零散,仅为自己记录。

Filter 和Interceptor

Filter:

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
public class AuthFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
ConfigProperties config = SpringUtil.getBean(ConfigProperties.class);
...
if (cookie) {
// 查看用户数据是否存在;用户是否禁用,更新用户使用时间点
chain.doFilter(request, response);
} else {
response.getWriter().write("no authenticate");
res.setStatus(HttpStatus.UNAUTHORIZED.value());
}
}
}
// 然后在FilterConfig中进行绑定,并设置中间件前后置
@Configuration
public class FilterConfig {
@Bean public FilterRegistrationBean registerAuthFilter() {
FilterRegistrationBean<AuthFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new AuthFilter());
registration.addUrlPatterns("/*");
registration.setName("AuthFilter");
registration.setOrder(1);
return registration;
}
}

Interceptor:

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
// 先创建一个annotation:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RoutePermissions {
String permissions() default "public";
}
// 创建一个interceptor
public class RoutePermissionsInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
RoutePermissions routePermissions = null;
if (handler instanceof HandlerMethod) {
routePermissions = (RoutePermissions)((HandlerMethod)handler).getMethodAnnotation(RoutePermissions.class);
// 如果该配置项没有permissions, 默认为admin,谁都可以进入
// TODO 该用户是否为admin
if (routePermissions == null) {
return true;
}
String permissions = routePermissions.permissions();
if (!StringUtils.isEmpty(permissions)) {
// TODO logic
// 判断是否有权限
return true;
} else {
response.getWriter().write("no permissions");
response setStatus(HttpStatus.FORBIDDEN.value());
return false;
}
}
return true;
}
}
// 最后在WebMvcConf中注册下
@Configuration
public class WebMvcConf implements WebMvcConfigurer {
@Bean public RoutePermissionsInterceptor myRoutePermissionInterceptor() {
RoutePermissionsInterceptor interceptor = new RoutePermissionsInterceptor();
return interceptor;
}
@Override public void addInterceptor(InterceptorRegistry registry) {
registry.addInterceptor(myRoutePermissionInterceptor());
...registry.addInterceptor(...other interceptors);
}
}

:: 作用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.forEach(System.out::println);

// 其中list.forEach可以改写成以下代码:
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 或者等于以下代码:
for (String s : list) {
System.out.println(s);
}

抄录-java中的::是什么意思? - 无措的回答 - 知乎

Lambda表达式和函数式接口

仅仅有一个未实现方法的接口,可以直接写作(参数列表)->{方法体}这种形式

例如:

1
2
3
4
@FunctionalInterface
public interface FuncA {
void doSomething(String str);
}

那么上面这种接口就可以直接写作:

FuncA funcA = (str)-> {
  System.out.println("hello");
}

同时System.out.println 的参数和传递的参数element的类型完全匹配,所以可以简化为:

.forEach(System.out::println)