ssm后台管理系统-05

1.登录

需求分析:前台发送登录请求,首先比较验证码,验证码作用就是防止频繁访问数据库,如果验证码错误就会返回到登录页面并提示,账号密码错误时也会返回登录页面,并提示账号密码错误。

1.controller

这里使用session绑定登录信息:服务器不关,绑定的数据不会消失

使用request绑定登录报错信息:数据绑定的数据只能存在一次请求中。

@Controller
@RequestMapping("/user")
public class Home {
    @Autowired
    EmpService empService;
    @RequestMapping("/login")
    public String login(Employee employee, HttpServletRequest request)
    {
        
      List< Employee> employee1= empService.getOneByNameAndPassword(employee);
       //获取前台输入的验证码
        String code=employee.getCode();
        //获取随机生成的验证码
        String getCode= (String) request.getSession().getAttribute(RandomValidateCode.RANDOMCODEKEY);
        //判断验证码
        if(code.equalsIgnoreCase(getCode))
        {
            if(!employee1.isEmpty())
            {
                request.getSession().setAttribute("userInfo",employee);//使用session绑定
                return "main";
            }
            else
            {
                request.setAttribute("code","账号或者密码错误");
                return "forward:/login.jsp";
            }
        }
        else
        {
            request.setAttribute("code","验证码错误");
            return "redirect:/login.jsp";
        }


    }

}

2,service

根据条件查询,设立账号和密码为查询条件。

 3.配置登录拦截器

第一步:在springmvc配置文件中写拦截的配置

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/emp/*"/>//拦截的请求
        <bean class="com.buka.edu.filter.LoginFilter"/>//哪个拦截类
    </mvc:interceptor>
</mvc:interceptors>

2.filter类实现HandlerInterceptor

通过查询session是否绑定值,来确定是否登录过。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值