1、CrosFilter
package 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
package 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
package 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
package 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); } }