蓝缘基础框架项目-登录授权

今天看下登录的情况:

首先页面元素代码如下:

<div class="login">
    <div class="box png">
    <div class="logo png"></div>
    <div class="input">
      <div class="log">
      <form id="loginForm" name="loginForm" method="post" action="${pageContext.servletContext.contextPath }/login.html">
        <div class="name">
          <label>用户名</label><input type="text" class="text" id="username" placeholder="用户名" name="username" tabindex="1">
        </div>
        <div class="pwd">
          <label>密 码</label><input type="password" class="text" id="password" placeholder="密码" name="password" tabindex="2">
          <input type="button" class="submit" tabindex="3" value="登录" onclick="checkUserForm();">
          <div class="check"></div>
        </div>
        <div class="tip"></div>
        </form>
      </div>
    </div>
  </div>
    <div class="air-balloon ab-1 png"></div>
  <div class="air-balloon ab-2 png"></div>
    <div class="footer"></div>
</div>

说明:

1、点击登录按钮,触发checkUserForm 验证表单事件。

2、当表单验证成功后才进行登录,调用后端登录接口。

3、表单验证内容:判断用户名或密码是否为空。


function checkUserForm() {
  var userName = $("#username").val();
  var userPassword = $("#password").val();
  if (userName == "" || userPassword == "") {
    alert("用户名或密码不能为空");
    return false;
  }
  document.loginForm.submit();
}

4、为了显示调用登录后的结果,在初始化页面的时候判断错误信息是否为空,若不为空,则弹出错误提示信息。

$(function(){
    var error =  "${error}";
    if (error != ''){
         alert(error);
    }
 })

后端登录接口逻辑:

@RequestMapping(value = "login",method = {RequestMethod.POST})
    public String login(String username, String password, HttpServletRequest request) throws Exception {
        try {
            if (Common.isEmpty(username) || Common.isEmpty(password)) {
                request.setAttribute("error", "用户名或密码不能为空");
                return Common.BACKGROUND_PATH + "/framework/login";
            }

            Account account = new Account();
            account.setAccountName(username);
            account.setPassword(Md5Tool.getMd5(password));
            // 验证用户账号与密码是否正确
            Account users = accountService.countAccount(account);
            if (users == null) {
                request.setAttribute("error", "用户或密码不正确");
                return Common.BACKGROUND_PATH + "/framework/login";
            }

            Authentication authentication = myAuthenticationManager
                    .authenticate(new UsernamePasswordAuthenticationToken(username, users.getPassword()));
            SecurityContext securityContext = SecurityContextHolder.getContext();
            securityContext.setAuthentication(authentication);

            request.getSession().setAttribute(Common.SPRING_SECURITY_CONTEXT, securityContext);
            // 当验证都通过后,把用户信息放在session里
            request.getSession().setAttribute(Common.USER_SESSION, users);
            request.getSession().setAttribute(Common.USER_SESSION_ID, users.getId());

            String userName = users.getAccountName();
            String ip = Common.toIpAddr(request);

            UserLogin userLogin = new UserLogin();
            userLogin.setUserId(users.getId());
            userLogin.setUserName(userName);
            userLogin.setloginIP(ip);
            userLoginService.add(userLogin);

            request.removeAttribute("error");
        } catch (AuthenticationException ae) {
            logger.error(ae.getMessage());
            request.setAttribute("error", "登录异常,请联系管理员");
            return Common.BACKGROUND_PATH + "/framework/login";
        }
        return "redirect:index.html";
    }

说明:

1、调用登录接口,表单处理post请求。首先判断上送用户名或密码不能为空。

2、对密码进行md5加密,通过用户名和密码查询是否存在该用户。

3、springsecurity进行授权认证。

4、用户信息保存到session。

5、记录用户的登录日志表,并移除错误信息,转发进入系统主页面。

6、若发生错误则设置错误信息,跳转到登录页。

7、对于get请求,直接跳转到登录页面。

@RequestMapping(value = "login", method = {RequestMethod.GET})
    public String login(){
        return Common.BACKGROUND_PATH + "/framework/login";
    }

在这里插入图片描述

软件定制及其他业务

请加微信号:13128600812

公众号:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张先生程序猿

谢谢您的打赏,我会持续创作下去

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值