diff --git a/README.md b/README.md index 8a4725a..7b08180 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,14 @@ * 登录功能(支持用户名、邮箱登录) * 注册用户(通过邮箱注册) -* 重置密码(通过邮箱重置密码) \ No newline at end of file +* 重置密码(通过邮箱重置密码) + +登录功能: +1. 用户登录成之后,才能访问index路径下的页面 +2. 用户如果没有登录,那么会自动跳转到登录界面 +3. 如果用户请求的是一个压根就不存在的页面,依然强制回到登录界面,如果已经登录,那么回到index首页 + +登录解决方案 +1. 无论是否已经登录,直接向后端请求用户信息 +2. 如果请求成功,那么说明肯定是已经登录了 +3. 如果请求失败,那么说明没有登录,跳转到登录界面 \ No newline at end of file diff --git a/study-project-backend/src/main/java/com/example/config/WebConfiguration.java b/study-project-backend/src/main/java/com/example/config/WebConfiguration.java new file mode 100644 index 0000000..073b3cf --- /dev/null +++ b/study-project-backend/src/main/java/com/example/config/WebConfiguration.java @@ -0,0 +1,21 @@ +package com.example.config; + +import com.example.interceptor.AuthorizeInterceptor; +import jakarta.annotation.Resource; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfiguration implements WebMvcConfigurer { + + @Resource + AuthorizeInterceptor interceptor; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry + .addInterceptor(interceptor) + .addPathPatterns("/**"); + } +} diff --git a/study-project-backend/src/main/java/com/example/controller/UserController.java b/study-project-backend/src/main/java/com/example/controller/UserController.java new file mode 100644 index 0000000..166ecf2 --- /dev/null +++ b/study-project-backend/src/main/java/com/example/controller/UserController.java @@ -0,0 +1,18 @@ +package com.example.controller; + +import com.example.entity.RestBean; +import com.example.entity.user.AccountUser; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.SessionAttribute; + +@RestController +@RequestMapping("/api/user") +public class UserController { + + @GetMapping("/me") + public RestBean me(@SessionAttribute("account") AccountUser user){ + return RestBean.success(user); + } +} diff --git a/study-project-backend/src/main/java/com/example/entity/Account.java b/study-project-backend/src/main/java/com/example/entity/auth/Account.java similarity index 79% rename from study-project-backend/src/main/java/com/example/entity/Account.java rename to study-project-backend/src/main/java/com/example/entity/auth/Account.java index aece350..c6f0a2a 100644 --- a/study-project-backend/src/main/java/com/example/entity/Account.java +++ b/study-project-backend/src/main/java/com/example/entity/auth/Account.java @@ -1,4 +1,4 @@ -package com.example.entity; +package com.example.entity.auth; import lombok.Data; diff --git a/study-project-backend/src/main/java/com/example/entity/user/AccountUser.java b/study-project-backend/src/main/java/com/example/entity/user/AccountUser.java new file mode 100644 index 0000000..ae676e6 --- /dev/null +++ b/study-project-backend/src/main/java/com/example/entity/user/AccountUser.java @@ -0,0 +1,10 @@ +package com.example.entity.user; + +import lombok.Data; + +@Data +public class AccountUser { + int id; + String username; + String email; +} \ No newline at end of file diff --git a/study-project-backend/src/main/java/com/example/interceptor/AuthorizeInterceptor.java b/study-project-backend/src/main/java/com/example/interceptor/AuthorizeInterceptor.java new file mode 100644 index 0000000..6dc2dc8 --- /dev/null +++ b/study-project-backend/src/main/java/com/example/interceptor/AuthorizeInterceptor.java @@ -0,0 +1,30 @@ +package com.example.interceptor; + +import com.example.entity.user.AccountUser; +import com.example.mapper.UserMapper; +import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.User; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +@Component +public class AuthorizeInterceptor implements HandlerInterceptor { + @Resource + UserMapper mapper; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + SecurityContext context = SecurityContextHolder.getContext(); + Authentication authentication = context.getAuthentication(); + User user = (User)authentication.getPrincipal(); + String username = user.getUsername(); + AccountUser account = mapper.findAccountUserByNameOrEmail(username); + request.getSession().setAttribute("account", account); + return true; + } +} diff --git a/study-project-backend/src/main/java/com/example/mapper/UserMapper.java b/study-project-backend/src/main/java/com/example/mapper/UserMapper.java index 29a4cc3..27ea155 100644 --- a/study-project-backend/src/main/java/com/example/mapper/UserMapper.java +++ b/study-project-backend/src/main/java/com/example/mapper/UserMapper.java @@ -1,6 +1,7 @@ package com.example.mapper; -import com.example.entity.Account; +import com.example.entity.auth.Account; +import com.example.entity.user.AccountUser; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @@ -11,6 +12,9 @@ public interface UserMapper { @Select("select * from db_account where username = #{text} or email = #{text}") Account findAccountByNameOrEmail(String text); + @Select("select * from db_account where username = #{text} or email = #{text}") + AccountUser findAccountUserByNameOrEmail(String text); + @Insert("insert into db_account (email, username, password) values (#{email}, #{username}, #{password})") int createAccount(String username, String password, String email); diff --git a/study-project-backend/src/main/java/com/example/service/impl/AuthorizeServiceImpl.java b/study-project-backend/src/main/java/com/example/service/impl/AuthorizeServiceImpl.java index 34e2898..54a8b86 100644 --- a/study-project-backend/src/main/java/com/example/service/impl/AuthorizeServiceImpl.java +++ b/study-project-backend/src/main/java/com/example/service/impl/AuthorizeServiceImpl.java @@ -1,6 +1,6 @@ package com.example.service.impl; -import com.example.entity.Account; +import com.example.entity.auth.Account; import com.example.mapper.UserMapper; import com.example.service.AuthorizeService; import jakarta.annotation.Resource;