初始化
commit
aaae256ff3
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="demo11" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,36 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="JavaDoc" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="TOP_LEVEL_CLASS_OPTIONS">
|
||||
<value>
|
||||
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
||||
<option name="REQUIRED_TAGS" value="" />
|
||||
</value>
|
||||
</option>
|
||||
<option name="INNER_CLASS_OPTIONS">
|
||||
<value>
|
||||
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
||||
<option name="REQUIRED_TAGS" value="" />
|
||||
</value>
|
||||
</option>
|
||||
<option name="METHOD_OPTIONS">
|
||||
<value>
|
||||
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
||||
<option name="REQUIRED_TAGS" value="@return@param@throws or @exception" />
|
||||
</value>
|
||||
</option>
|
||||
<option name="FIELD_OPTIONS">
|
||||
<value>
|
||||
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
||||
<option name="REQUIRED_TAGS" value="" />
|
||||
</value>
|
||||
</option>
|
||||
<option name="IGNORE_DEPRECATED" value="false" />
|
||||
<option name="IGNORE_JAVADOC_PERIOD" value="true" />
|
||||
<option name="IGNORE_DUPLICATED_THROWS" value="false" />
|
||||
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
|
||||
<option name="myAdditionalJavadocTags" value="date" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4" />
|
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>demo11</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<version>2.7.15</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,12 @@
|
|||
package com.bwie;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package com.bwie.Filter;
|
||||
|
||||
|
||||
import com.bwie.config.LoginUserMap;
|
||||
import com.bwie.domian.UserInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
|
||||
@Slf4j
|
||||
//注册到容器中
|
||||
@Component
|
||||
//1、实现Filter,然后重写init,doFilter,destroy方法
|
||||
public class TokenFilter implements Filter {
|
||||
|
||||
|
||||
|
||||
|
||||
String WHILE_LIST="/login";
|
||||
String TOKEN_USER_KEY="token:user:";
|
||||
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
Filter.super.init(filterConfig);
|
||||
}
|
||||
|
||||
|
||||
//过滤方法
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
//先将其转成HttpServletRequest
|
||||
HttpServletRequest request=(HttpServletRequest) servletRequest;
|
||||
|
||||
String requestURI = request.getRequestURI();
|
||||
//如果不在白名单中,则检测token是否正常
|
||||
if(!WHILE_LIST.contains(requestURI)){
|
||||
|
||||
//获取请求头的参数
|
||||
String token = request.getHeader("token");
|
||||
if(token == null || token.length() == 0){
|
||||
resp(servletResponse,"账号未登录");
|
||||
return;
|
||||
}
|
||||
|
||||
//从redis中获取token是否存在,是否过期
|
||||
UserInfo userInfo = LoginUserMap.get(token);
|
||||
if(userInfo == null){
|
||||
resp(servletResponse,"账号未登录");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
Filter.super.destroy();
|
||||
}
|
||||
|
||||
|
||||
//相应方法封装
|
||||
private void resp(ServletResponse servletResponse,String msg) throws IOException {
|
||||
HttpServletResponse response=(HttpServletResponse) servletResponse;
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
response.setStatus(401);
|
||||
response.setContentType("application/json");
|
||||
outputStream.write(msg.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.bwie.config;
|
||||
|
||||
import com.bwie.domian.UserInfo;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class LoginUserMap {
|
||||
|
||||
private static final Map<String, UserInfo> userMap= new HashMap<>();
|
||||
|
||||
|
||||
public static void put(String token,UserInfo userInfo){
|
||||
userMap.put(token,userInfo);
|
||||
}
|
||||
|
||||
public static UserInfo get(String token){
|
||||
return userMap.get(token);
|
||||
}
|
||||
|
||||
public static void remove(String token){
|
||||
userMap.remove(token);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.bwie.config;
|
||||
|
||||
import com.bwie.Filter.TokenFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
//@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private TokenFilter tokenFilter;
|
||||
|
||||
|
||||
/**
|
||||
* 登录过滤器
|
||||
* 如果这个没有配置的话,默认所有的请求都会走filter
|
||||
*/
|
||||
@Bean
|
||||
public FilterRegistrationBean<Filter> loginFilterRegistration(){
|
||||
FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
|
||||
//设置过滤器
|
||||
registrationBean.setFilter(tokenFilter);
|
||||
registrationBean.setName("loginFilter");
|
||||
//拦截路径,这个就不大好,每次新增接口都得添加新的拦截器
|
||||
registrationBean.addUrlPatterns("/test/get");
|
||||
//指定顺序,数字越小越靠前
|
||||
registrationBean.setOrder(-1);
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.bwie.controller;
|
||||
|
||||
import com.bwie.config.LoginUserMap;
|
||||
import com.bwie.domian.UserInfo;
|
||||
import com.bwie.domian.req.LoginReq;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/login")
|
||||
public class LoginController {
|
||||
|
||||
|
||||
@PostMapping
|
||||
public String login(@RequestBody LoginReq loginReq){
|
||||
|
||||
if (!(loginReq.getUserName().length()>2 && loginReq.getPassword().length()>4)){
|
||||
return "登录失败";
|
||||
}
|
||||
String string = UUID.randomUUID().toString();
|
||||
LoginUserMap.put(string, UserInfo.LoginReqBuild(string,loginReq));
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping
|
||||
public String logout(@RequestHeader("token") String token){
|
||||
LoginUserMap.remove(token);
|
||||
return "退出成功";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.bwie.controller;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Log4j2
|
||||
@RestController
|
||||
@RequestMapping("/test")
|
||||
public class TestController {
|
||||
@PostMapping
|
||||
public String post(@RequestBody String str){
|
||||
log.info("接收到请求:{}",str);
|
||||
return "请求成功";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.bwie.domian;
|
||||
|
||||
import com.bwie.domian.req.LoginReq;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UserInfo {
|
||||
public String userId;
|
||||
public String userName;
|
||||
private String password;
|
||||
|
||||
public static UserInfo LoginReqBuild(String userId, LoginReq loginReq) {
|
||||
return UserInfo.builder()
|
||||
.userId(userId)
|
||||
.userName(loginReq.getUserName())
|
||||
.password(loginReq.getPassword()).build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.bwie.domian.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginReq {
|
||||
|
||||
private String userName;
|
||||
private String password;
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,3 @@
|
|||
artifactId=demo11
|
||||
groupId=com.bwie
|
||||
version=1.0-SNAPSHOT
|
|
@ -0,0 +1,9 @@
|
|||
com\bwie\config\WebMvcConfig.class
|
||||
com\bwie\controller\LoginController.class
|
||||
com\bwie\domian\UserInfo.class
|
||||
com\bwie\Filter\TokenFilter.class
|
||||
com\bwie\controller\TestController.class
|
||||
com\bwie\domian\req\LoginReq.class
|
||||
com\bwie\domian\UserInfo$UserInfoBuilder.class
|
||||
com\bwie\config\LoginUserMap.class
|
||||
com\bwie\App.class
|
|
@ -0,0 +1,6 @@
|
|||
D:\Project\demo11\src\main\java\com\bwie\config\LoginUserMap.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\domian\req\LoginReq.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\domian\UserInfo.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\controller\LoginController.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\controller\TestController.java
|
||||
D:\Project\demo11\src\main\java\com\bwie\App.java
|
Loading…
Reference in New Issue