Tia*_*ues 5 subject principal shiro
我正在使用 apache shiro。当我想知道用户是否有权限和角色时,我使用 SecutiryUtils.getSubject()。我想知道如何向主题添加更多信息,例如电子邮件、主键和我需要的任何其他业务信息,以便在必要时检索该信息。
这是我的 shiro.ini:
[main]
ds = org.apache.shiro.jndi.JndiObjectFactory
ds.requiredType = javax.sql.DataSource
ds.resourceName = java:/comp/env/jdbc/myDS
# JDBC realm config
jdbcRealm = com.mycompany.JdbcRealmImpl
jdbcRealm.permissionsLookupEnabled = true
jdbcRealm.authenticationQuery = SELECT password FROM user WHERE username = ? AND status = 1
jdbcRealm.dataSource = $ds
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
jdbcRealm.credentialsMatcher = $sha256Matcher
[urls]
/logout = logout
/** = authcBasic
Run Code Online (Sandbox Code Playgroud)
这是我的 JdbcRealm
public class JdbcRealmImpl extends JdbcRealm {
public JdbcRealmImpl() {
super();
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
final AuthenticationToken token) throws AuthenticationException {
final AuthenticationInfo info = super.doGetAuthenticationInfo(token);
// create a user to test
final User user = new User();
user.setId(11111);
return new SimpleAuthenticationInfo(user, info.getCredentials(),
getName());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试检索用户信息的代码。
final Subject currentUser = SecurityUtils.getSubject();
final User user = (User) currentUser.getPrincipal();
// null
System.out.println(user);
Run Code Online (Sandbox Code Playgroud)
您应该将其放入数据库中并使用主题用户名(例如电子邮件地址)检索它。
| 归档时间: |
|
| 查看次数: |
1995 次 |
| 最近记录: |