mirror of
https://github.com/itbaima-study/SpringBoot-Vue-Template-Session.git
synced 2025-05-17 13:51:15 +08:00
完成用户信息查询功能
This commit is contained in:
parent
88ce641fda
commit
b03fb8fdb5
12
README.md
12
README.md
@ -4,4 +4,14 @@
|
||||
|
||||
* 登录功能(支持用户名、邮箱登录)
|
||||
* 注册用户(通过邮箱注册)
|
||||
* 重置密码(通过邮箱重置密码)
|
||||
* 重置密码(通过邮箱重置密码)
|
||||
|
||||
登录功能:
|
||||
1. 用户登录成之后,才能访问index路径下的页面
|
||||
2. 用户如果没有登录,那么会自动跳转到登录界面
|
||||
3. 如果用户请求的是一个压根就不存在的页面,依然强制回到登录界面,如果已经登录,那么回到index首页
|
||||
|
||||
登录解决方案
|
||||
1. 无论是否已经登录,直接向后端请求用户信息
|
||||
2. 如果请求成功,那么说明肯定是已经登录了
|
||||
3. 如果请求失败,那么说明没有登录,跳转到登录界面
|
@ -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("/**");
|
||||
}
|
||||
}
|
@ -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<AccountUser> me(@SessionAttribute("account") AccountUser user){
|
||||
return RestBean.success(user);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.example.entity;
|
||||
package com.example.entity.auth;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -0,0 +1,10 @@
|
||||
package com.example.entity.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AccountUser {
|
||||
int id;
|
||||
String username;
|
||||
String email;
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user