spring security 自定义登陆 - AJAX

27 篇文章 0 订阅

除了自定义登陆页面外,我们经常会需要处理ajax登陆 

(自定义登陆参考: http://blog.csdn.net/buyaore_wo/article/details/50056353 )


1.修改LoginPage

	@Override
	protected void configure(HttpSecurity http) throws Exception {

		// @formatter:off
		
        http
            .authorizeRequests()
                .antMatchers(PermitAllPages).permitAll()
                .anyRequest().hasRole("USER")
                .and()
            .exceptionHandling()
                .accessDeniedPage("/access/denied")
                .and()
            // TODO: put CSRF protection back into this endpoint
            .csrf()
                .disable()
            .logout()
            	.logoutUrl("/logout")
                .logoutSuccessUrl("/login.jsp")
                .and()
            .formLogin()
            	.loginPage("/loginPage")//登陆页面
            	.loginProcessingUrl("/login")//登陆处理路径
            	.usernameParameter("username")//登陆用户名参数
            	.passwordParameter("password")//登陆密码参数
            	.defaultSuccessUrl("/login/success")//登陆成功路径
                .failureUrl("/login/failure");//登陆失败路径
        // @formatter:on

	}

2. ajax登陆处理

	@RequestMapping("loginPage")
	public String loginPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
		if (HttpUtils.isAjaxRequest(httpServletRequest)) {
			return "forward:/loginPageAjax";
		} else {
			return "forward:/login.html";
		}
	}

	@RequestMapping("loginPageAjax")
	public @ResponseBody HttpJsonResponse loginPageAjax() {
		return new HttpJsonResponse("-1", "need to login");
	}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值