public class UserFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
// 判断当前session是否有用户信息
if(req.getSession().getAttribute("user") == null) {
//保存当前客户想要去的url地址
String goURL = req.getServletPath();//获得用户想要去的地址
String param = req.getQueryString(); //获得地址中携带的参数
if(param != null) {
goURL = goURL + "?" + param; //重新拼好请求地址+参数
}
//把当前客户想要访问的地址,存储到session中
req.getSession().setAttribute("goURL", goURL);
//非法请求,跳转到登陆页面
req.getSession().setAttribute("error", "非法请求,请登录!");
res.sendRedirect(req.getContextPath() + "/ulogin.jsp");
} else {
//如果有下一个过滤器则跳转,否则直接到目标页面
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
}
}
@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction<User> {
public String login() {
//进行登陆的判断
model = userService.login(model);
if(model == null) {
session.put("error", "登陆失败");
return "login";
} else {
//登录成功,先将用户存储到session中
session.put("user", model);
//根据session中goURL是否有值而决定页面的跳转
if(session.get("goURL") == null) {
return "index"; //跳到首页
} else {
return "goURL";
}
}
}
}
//userService接口
public interface UserService extends BaseService<User> {
//用户登陆,成功返回该User
public User login(User user);
}
//userServiceImpl实现类
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements
UserService {
@Override
public User login(User user) {
String hql = "from User u where u.login=:login and u.pass=:pass";
return (User) getSession().createQuery(hql) //
.setString("login", user.getLogin()) //
.setString("pass", user.getPass()) //
.uniqueResult();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有