springsecurity ajax登录

springsecurity ajax登录

登录页面

<!DOCTYPE html>
<html lang="en" >
<script src="webjars/jquery/3.5.1/jquery.min.js"></script>
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>

<div style="text-align: center">

        登录名:<input id="name" /> <br/>
        密码:<input type="password" id="passwd"/><br/>
       <input type="checkbox" id="jzw"> 记住我<br/>
        <input type="button" value="登录" id="login"/>
</div>
</body>
<script type="application/javascript">
    $("#login").click(function () {
    
    
        var name=$("#name").val();
        var passwd=$("#passwd").val();
        var jzw=$("#jzw").val();
        $.ajax({
    
    
            url:"/login",
            type:"POST",
            dataType:"json",
            data:{
    
    
                name:name,
                passwd:passwd,
                jzw:jzw
            },
            success: function (resq) {
    
    
                if(resq.success){
    
    
                    window.location.href="/index";
                }
            }
        })
    })
</script>
</html>

封装请求响应类

public class Result {
    
    
    private static final String SUCCESS = "success";

    private static final String MESSAGE = "message";
    
    public static Map<String, Object> success(String message) {
    
    
        Map<String, Object> map = new HashMap<>();
        map.put(SUCCESS, true);
        map.put(MESSAGE, message);
        return map;
    }
    public static Map<String, Object> success(Object object) {
    
    
        Map<String, Object> map = new HashMap<>();
        map.put(SUCCESS, true);
        map.put(MESSAGE, object);
        return map;
    }
    public static Map<String, Object> success(String message, Map<String, Object> data) {
    
    
        Map<String, Object> map = Result.success(message);
        for (Map.Entry<String, Object> entrySet : data.entrySet()) {
    
    
            String key = entrySet.getKey();
            Object value = entrySet.getValue();
            map.put(key, value);
        }
        return map;
    }
    public static Map<String, Object> fail(String message) {
    
    
        Map<String, Object> map = new HashMap<>();
        map.put(SUCCESS, false);
        map.put(MESSAGE, message);
        return map;
    }

}

设置springsecurity配置异步登录

     //认证
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
    
        http.authorizeRequests()
                .antMatchers("/","/index","/toLogin","/login").permitAll()
                .antMatchers("/level1/**").hasRole("level1")
                .antMatchers("/level2/**").hasRole("level2")
                .antMatchers("/level3/**").hasRole("level3");
        http.formLogin().loginPage("/toLogin")  //指定自定义的登录页面
                .usernameParameter("name")      //指定自定义的登录页面用户名
                .passwordParameter("passwd")    //指定自定义的登录页面密码
                .loginProcessingUrl("/login")   //ajax提交的路径
                .successHandler(new SimpleUrlAuthenticationSuccessHandler() {
    
    
            @Override
            public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
    
    
                httpServletResponse.setContentType("application/json;charset=utf-8");
                ServletOutputStream out = httpServletResponse.getOutputStream();
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.writeValue(out, Result.success("登录成功"));
                out.flush();
                out.close();
            }
        })
               .failureHandler(new SimpleUrlAuthenticationFailureHandler() {
    
    
                   @Override
                   public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
    
    
                       httpServletResponse.setContentType("application/json;charset=utf-8");
                       ServletOutputStream out = httpServletResponse.getOutputStream();
                       ObjectMapper objectMapper = new ObjectMapper();
                       objectMapper.writeValue(out, Result.success("登录失败"));
                       out.flush();
                       out.close();
                   }
               });
        http.csrf().disable();
    }

猜你喜欢

转载自blog.csdn.net/weixin_45742032/article/details/114089315
今日推荐