springboot shiro ajax,SpringBoot Shiro 登录成功后返回json数据 shiro使用ajax登录

老规矩,先上代码:

protected boolean onLoginSuccess(AuthenticationToken token, Subject subject,

ServletRequest request, ServletResponse response) throws Exception {

//WebUtils.redirectToSavedRequest(request, response, JSON.toJSONString(ResultUtil.success()));

// response.reset();

response.setContentType("application/json");

response.setCharacterEncoding("utf-8");

response.getWriter().print(JSON.toJSONString(ResultUtil.success()));

return false;

}

注意事项:onLoginSuccess会将用户的session_id写入head里面进行set-cookie,但是网络上很多使用了response.reset();将头部信息清空了,session写入失败后前台就不会登陆成功。

一、定义登录过滤类

自定义:SunckFormAuthenticationFilter 继承 FormAuthenticationFilter

/**

* 表单过滤类(验证码)

*

* @author LengChen

* @version 1.0

* @date 2020/8/18

*/

public class SunckFormAuthenticationFilter extends FormAuthenticationFilter {

/**

* 登录认证成功后回调方法

* @param request

* @param response

* @throws Exception

*/

protected boolean onLoginSuccess(AuthenticationToken token, Subject subject,

ServletRequest request, ServletResponse response) throws Exception {

//WebUtils.redirectToSavedRequest(request, response, JSON.toJSONString(ResultUtil.success()));

// response.reset();

response.setContentType("application/json");

response.setCharacterEncoding("utf-8");

response.getWriter().print(JSON.toJSONString(ResultUtil.success()));

return false;

}

/**

* 登录失败后回调方法

*

*/

protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e,

ServletRequest request, ServletResponse response) {

String className = e.getClass().getName(), message = "";

System.out.println(className);

if (IncorrectCredentialsException.class.getName().equals(className) || AuthenticationException.class.getName().equals(className)

|| UnknownAccountException.class.getName().equals(className)){

if(!"-1".equals(StringUtils.isNumeric(e.getMessage()))){

message = e.getMessage();

}else{

message = ResultEnum.SHIRO_ERROR_4.getStringCode();

}

}

else if (StringUtils.isNotBlank(e.getMessage())) {

message = e.getMessage();

}

else{

message = ResultEnum.UNKNOWN_ERROR.getStringCode();

e.printStackTrace(); // 输出到控制台

}

request.setAttribute(getFailureKeyAttribute(), className);

request.setAttribute(Global.DEFAULT_MESSAGE_PARAM, message);

return true;

}

}

二、shiro配置登录过滤器

shiroFilterChainDefinitions.put("/login", "authc");

//自定义拦截器

Mapfilters = shiroFilterFactoryBean.getFilters();

filters.put("authc", new SunckFormAuthenticationFilter());

注:这里配置了之后,login的post登录会首先进入该过滤器报错后才会继续走controller,如果登录成功会直接进行输

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中使用 Shiro 实现免密登录的步骤如下: 1. 导入 Shiro 和 Spring Boot 的相关依赖。在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-starter</artifactId> <version>1.7.1</version> </dependency> ``` 2. 创建一个自定义的 ShiroRealm 类,并继承 org.apache.shiro.realm.AuthenticatingRealm 类。在该类中重写 doGetAuthenticationInfo 方法,用于验证用户身份信息。这里可以通过用户名和密码进行验证,也可以通过其他方式实现免密登录。 3. 在 application.properties 或 application.yml 文件中配置 Shiro 的相关属性。例如,配置 Shiro 使用的 Realm 类和登录 URL: ```yaml shiro: authc: loginUrl: /login realms: myRealm: authenticationTokenClass: org.apache.shiro.authc.UsernamePasswordToken ``` 4. 在 Spring Boot 的入口类上添加 @EnableShiro 注解,启用 Shiro 功能。 5. 创建一个登录接口,处理用户登录请求。在该接口中,可以使用 Shiro 的 Subject 类来进行登录验证。例如: ```java @PostMapping("/login") public String login(String username, String password) { Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, password); try { subject.login(token); return "登录成功"; } catch (AuthenticationException e) { return "用户名或密码错误"; } } ``` 这样,当用户访问登录接口并提供正确的用户名和密码时,就可以实现免密登录,并返回登录成功的提示信息。当用户提供错误的用户名或密码时,将返回错误提示信息。 注意:以上只是简单示例,实际应用中可能需要更复杂的逻辑和安全措施。建议参考 Shiro 的官方文档和示例代码来进行具体的实现和配置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值