1、CrosFilter
javapackage com.csdn.headline.filters; import jakarta.servlet.*; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; public class CrosFilter implements Filter { /** * 过滤器方法,用于处理HTTP请求 * @param servletRequest Servlet请求对象 * @param servletResponse Servlet响应对象 * @param filterChain 过滤器链 * @throws IOException 如果发生IO异常 * @throws ServletException 如果发生Servlet异常 */ @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { // 将响应对象转换为HttpServletResponse类型 HttpServletResponse response = (HttpServletResponse) servletResponse; // 设置Access-Control-Allow-Origin头部字段为"*",表示允许所有来源访问 response.setHeader("Access-Control-Allow-Origin", "*"); // 设置Access-Control-Allow-Methods头部字段为"POST, GET, OPTIONS, DELETE, HEAD",表示支持的请求方法 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD"); // 设置Access-Control-Max-Age头部字段为"3600",表示预检请求的最大缓存时间 response.setHeader("Access-Control-Max-Age", "3600"); // 设置Access-Control-Allow-Headers头部字段为"access-control-allow-origin, authority, content-type, version-info, X-Requested-With",表示支持的请求头 response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); // 调用过滤器链的doFilter方法,将请求和响应对象传入 filterChain.doFilter(servletRequest, servletResponse); } }
2、NewsUserController
javapackage com.csdn.headline.controller; import com.csdn.headline.common.Result; import com.csdn.headline.common.ResultCodeEnum; import com.csdn.headline.pojo.NewsUser; import com.csdn.headline.service.NewsUserService; import com.csdn.headline.util.MD5Util; import com.csdn.mymvc.annotation.*; @Controller @RequestMapping("/user") public class NewsUserController { @Autowire private NewsUserService newsUserService; /** * 登录方法 * @param newsUser 用户信息 * @return 登录结果 */ @PostMapping("/login") public Result<NewsUser> login(@RequestBody NewsUser newsUser) { // 根据用户名从数据库中获取用户信息 NewsUser newsUserFromDB = newsUserService.getNewsUserByUserName(newsUser.getUsername()); // 如果数据库中没有找到对应用户名的用户信息,则返回登录失败的结果 if (newsUserFromDB == null) { return Result.build(null, ResultCodeEnum.USERNAME_ERROR); } else { // 获取数据库中用户密码 String pwdFromDB = newsUserFromDB.getUserPwd(); // 对输入的密码进行加密 String yourPwd = MD5Util.encrypt(newsUser.getUserPwd()); // 如果加密后的密码与数据库中的密码不一致,则返回登录失败的结果 if (!yourPwd.equals(pwdFromDB)) { return Result.build(null, ResultCodeEnum.PASSWORD_ERROR); } else { // 清空数据库中用户的密码,完成登录并返回用户信息 newsUserFromDB.setUserPwd(null); return Result.ok(newsUserFromDB); } } } }
3、NewsUserServiceImpl
javapackage com.csdn.headline.service.impl; import com.csdn.headline.dao.NewsUserDao; import com.csdn.headline.pojo.NewsUser; import com.csdn.headline.service.NewsUserService; import com.csdn.mymvc.annotation.Autowire; import com.csdn.mymvc.annotation.Service; @Service public class NewsUserServiceImpl implements NewsUserService { @Autowire private NewsUserDao newsUserDao; /** * 根据用户名获取对应的NewsUser对象 * @param userName 用户名 * @return 对应的NewsUser对象 */ @Override public NewsUser getNewsUserByUserName(String userName) { return newsUserDao.getNewsUserByUserName(userName); } }
4、NewsUserDaoImpl
javapackage com.csdn.headline.dao.impl; import com.csdn.headline.dao.NewsUserDao; import com.csdn.headline.pojo.NewsUser; import com.csdn.mymvc.annotation.Repository; import com.csdn.mymvc.dao.BaseDao; @Repository public class NewsUserDaoImpl extends BaseDao<NewsUser> implements NewsUserDao { @Override public NewsUser getNewsUserByUserName(String userName) { // 根据用户名查询对应的NewsUser对象 String sql = "select uid,username,user_pwd as userPwd,nick_name as nickName from news_user where username =?"; return load(sql, userName); } }
微头条项目实战:通过postman测试登录验证请求
丁总学Java2023-11-10 14:27
相关推荐
awonw6 小时前
[java][框架]springMVC(1/2)钱钱钱端9 小时前
【压力测试】如何确定系统最大并发用户数?迃幵chen11 小时前
wireshark-网络分析工具孤蓬&听雨12 小时前
RabbitMQ自动发送消息工具(自动化测试RabbitMQ)土小帽软件测试12 小时前
jmeter基础01-2_环境准备-Mac系统安装jdkqq_4337169515 小时前
测试分层:减少对全链路回归依赖的探索!qq_4337169516 小时前
Postman断言与依赖接口测试详解!如光照17 小时前
Linux与Windows中的流量抓取工具:wireshark与tcpdump土小帽软件测试17 小时前
jmeter基础03_汉化jmeter界面小白~小黑20 小时前
软件测试基础十二(python变量进阶)