1、shiro.xml配置认证成功跳转路径后再该方法中:
/**
* shiro认证后获取用户信息进入首页
* @param model
* @return
*/
@RequestMapping("index.do")
public String index(Model model){
Subject subject = SecurityUtils.getSubject();
/**
* 说明:
* 1、之所以能转成User,是在shiro的doGetAuthenticationInfo认证时传入的是user;
* 2、认证的实体类User必须实现序列化接口,并提供版本号
* 3、若传入的是username,则只能获取到username
*/
User user = (User)subject.getPrincipal();
System.out.println(user.getId()+"========="+user.getUsername()+"============"+user.getPassword());
model.addAttribute("username",user.getUsername());
return "/models/sys/index";
}
2、自定义Realm中
/**
* 认证
* @param token
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//token获取登录输入用户信息
String userName = token.getPrincipal().toString();
SimpleAuthenticationInfo info = null;
try {
if(loginService==null){
loginService = SpringBeanFactoryUtils.getBean(LoginService.class);
}
User user = loginService.getTokenByUser(userName);
info = new SimpleAuthenticationInfo(**user**, user.getPassword(),ByteSource.Util.bytes(user.getSalt()), getName());
}catch (Exception e){
e.printStackTrace();
}
return info;
}
3、实体类
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String username;
private String password;
private String salt;
}
本文档详细介绍了如何在Shiro认证成功后获取并处理登录用户信息,包括配置认证成功后的跳转路径,自定义Realm的实现以及实体类的设计与使用。通过对Shiro框架的深入理解,可以有效地管理和授权应用程序的用户。
1248

被折叠的 条评论
为什么被折叠?



