Search in sources :

Example 1 with User

use of io.nutz.demo.simple.bean.User in project nutzboot by nutzam.

the class SimpleAuthorizingRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    // null usernames are invalid
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }
    long userId = ((Number) principals.getPrimaryPrincipal()).longValue();
    User user = dao().fetch(User.class, userId);
    if (user == null)
        return null;
    SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo();
    auth.addRole(user.getName());
    auth.addStringPermission("user:list");
    return auth;
}
Also used : User(io.nutz.demo.simple.bean.User) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) AuthorizationException(org.apache.shiro.authz.AuthorizationException)

Example 2 with User

use of io.nutz.demo.simple.bean.User in project nutzboot by nutzam.

the class MainLauncher method newUser.

protected static User newUser(String name, String password) {
    User user = new User();
    user.setName(name);
    user.setSalt(R.UU32());
    user.setPassword(new Sha256Hash(password, user.getSalt()).toHex());
    user.setCreateTime(new Date());
    return user;
}
Also used : User(io.nutz.demo.simple.bean.User) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) Date(java.util.Date)

Example 3 with User

use of io.nutz.demo.simple.bean.User in project nutzboot by nutzam.

the class SimpleAuthorizingRealm method doGetAuthenticationInfo.

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    SimpleShiroToken upToken = (SimpleShiroToken) token;
    User user = dao().fetch(User.class, (Long) upToken.getPrincipal());
    if (user == null)
        return null;
    return new SimpleAccount(user.getId(), user.getPassword(), getName());
}
Also used : SimpleAccount(org.apache.shiro.authc.SimpleAccount) User(io.nutz.demo.simple.bean.User) SimpleShiroToken(org.nutz.integration.shiro.SimpleShiroToken)

Example 4 with User

use of io.nutz.demo.simple.bean.User in project nutzboot by nutzam.

the class MainLauncher method init.

public void init() {
    ZMoCo co = zmodb.cc("user", true);
    ZMo mo = ZMo.me();
    co.insert(mo.toDoc(new User("apple", 40, "北京")));
    co.insert(mo.toDoc(new User("apple", 40, "北京")));
    co.insert(mo.toDoc(new User("ball", 30, "未知")));
    co.insert(mo.toDoc(new User("cat", 50, "温哥华")));
    co.insert(mo.toDoc(new User("fox", 51, "纽约")));
    co.insert(mo.toDoc(new User("bra", 25, "济南")));
    co.insert(mo.toDoc(new User("lina", 50, "深圳")));
}
Also used : ZMoCo(org.nutz.mongo.ZMoCo) User(io.nutz.demo.simple.bean.User) ZMo(org.nutz.mongo.ZMo)

Example 5 with User

use of io.nutz.demo.simple.bean.User in project nutzboot by nutzam.

the class UserModule method login.

@Ok("json")
@Fail("http:500")
@POST
@At("/login")
public boolean login(@Param("username") String username, @Param("password") String password, HttpSession session) {
    User user = dao.fetch(User.class, username);
    if (user == null)
        return false;
    Sha256Hash hash = new Sha256Hash(password, user.getSalt());
    if (!hash.toHex().equals(user.getPassword())) {
        return false;
    }
    Subject subject = SecurityUtils.getSubject();
    subject.login(new SimpleShiroToken(user.getId()));
    return true;
}
Also used : User(io.nutz.demo.simple.bean.User) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) SimpleShiroToken(org.nutz.integration.shiro.SimpleShiroToken) Subject(org.apache.shiro.subject.Subject) At(org.nutz.mvc.annotation.At) POST(org.nutz.mvc.annotation.POST) Ok(org.nutz.mvc.annotation.Ok) Fail(org.nutz.mvc.annotation.Fail)

Aggregations

User (io.nutz.demo.simple.bean.User)5 Sha256Hash (org.apache.shiro.crypto.hash.Sha256Hash)2 SimpleShiroToken (org.nutz.integration.shiro.SimpleShiroToken)2 Date (java.util.Date)1 SimpleAccount (org.apache.shiro.authc.SimpleAccount)1 AuthorizationException (org.apache.shiro.authz.AuthorizationException)1 SimpleAuthorizationInfo (org.apache.shiro.authz.SimpleAuthorizationInfo)1 Subject (org.apache.shiro.subject.Subject)1 ZMo (org.nutz.mongo.ZMo)1 ZMoCo (org.nutz.mongo.ZMoCo)1 At (org.nutz.mvc.annotation.At)1 Fail (org.nutz.mvc.annotation.Fail)1 Ok (org.nutz.mvc.annotation.Ok)1 POST (org.nutz.mvc.annotation.POST)1