1、新增用户登录接口

This commit is contained in:
Hao Miao
2024-04-02 15:41:44 +08:00
parent 0ea3fe40c7
commit 8b8c5815cc
3 changed files with 63 additions and 4 deletions

View File

@@ -0,0 +1,22 @@
package com.realtime.protection.configuration.auth;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author Yixiang Zhao (@seriouszyx)
**/
@SpringBootApplication
@EnableCaching
public class Application implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration ir = registry.addInterceptor(new LoginInterceptor());
ir.addPathPatterns("/**");
ir.excludePathPatterns("/js/**", "/html/**", "/image/**", "/css/**", "/api/**");
}
}

View File

@@ -0,0 +1,31 @@
package com.realtime.protection.configuration.auth;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
import com.realtime.protection.configuration.entity.user.User;
import com.realtime.protection.configuration.response.ResponseResult;
/**
* @author Yixiang Zhao
**/
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if (user != null) {
return true;
}
// 未登录
throw new Exception("not login");
}
}

View File

@@ -2,10 +2,7 @@ package com.realtime.protection.server.user.login;
import com.realtime.protection.configuration.entity.user.User;
import com.realtime.protection.configuration.response.ResponseResult;
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;
import org.springframework.web.bind.annotation.*;
import javax.security.auth.login.LoginException;
@@ -36,4 +33,13 @@ public class LoginController {
.setData("userId", userId)
.setData("success", true);
}
@PostMapping("/auth")
public ResponseResult auth(@RequestParam("sessionData") String sessionData,
@RequestParam("accessToken") String accessToken,
@RequestParam(value = "scopes", required = false) String scopes) {
return ResponseResult.ok().setMessage("success")
.setData("success", true);
}
}