Search in sources :

Example 6 with SimpleAuthenticationInfo

use of org.apache.shiro.authc.SimpleAuthenticationInfo in project vip by guangdada.

the class ShiroFactroy method info.

@Override
public SimpleAuthenticationInfo info(ShiroUser shiroUser, User user, String realmName) {
    String credentials = user.getPassword();
    // 密码加盐处理
    String source = user.getSalt();
    ByteSource credentialsSalt = new Md5Hash(source);
    return new SimpleAuthenticationInfo(shiroUser, credentials, credentialsSalt, realmName);
}
Also used : SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) ByteSource(org.apache.shiro.util.ByteSource) Md5Hash(org.apache.shiro.crypto.hash.Md5Hash)

Example 7 with SimpleAuthenticationInfo

use of org.apache.shiro.authc.SimpleAuthenticationInfo in project Spring-Family by Sierou-Java.

the class MyShiroRealm method doGetAuthenticationInfo.

// ////////////////////////////////////////////////////////身份认证 START //////////////////////////////////////////////////////
/**
 * 认证信息.(身份验证)
 * :
 * Authentication 是用来验证用户身份
 * @param token
 * @return
 * @throws AuthenticationException
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    System.out.println("MyShiroRealm.doGetAuthenticationInfo()");
    // 获取用户的输入的账号.
    String username = (String) token.getPrincipal();
    System.out.println(token.getCredentials());
    // 通过username从数据库中查找 User对象,如果找到,没找到.
    // 实际项目中,这里可以根据实际情况做缓存,如果不做,Shiro自己也是有时间间隔机制,2分钟内不会重复执行该方法
    UserInfo userInfo = userInfoService.findByUsername(username);
    System.out.println("MyShiroRealm.doGetAuthenticationInfo():----->>userInfo=" + userInfo);
    if (userInfo == null) {
        return null;
    }
    /*
		 * 获取权限信息:这里没有进行实现,
		 * 请自行根据UserInfo,Role,Permission进行实现;
		 * 获取之后可以在前端for循环显示所有链接;
		 */
    // userInfo.setPermissions(userService.findPermissions(user));
    // 账号判断;
    // 加密方式;
    // 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配,如果觉得人家的不好可以自定义实现
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(// 用户名
    userInfo, // 密码
    userInfo.getPassword(), // salt=username+salt
    ByteSource.Util.bytes(userInfo.getCredentialsSalt()), // realm name
    getName());
    return authenticationInfo;
}
Also used : SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) UserInfo(org.family.pojo.UserInfo)

Example 8 with SimpleAuthenticationInfo

use of org.apache.shiro.authc.SimpleAuthenticationInfo in project shiro by apache.

the class JdbcRealm method doGetAuthenticationInfo.

/*--------------------------------------------
    |               M E T H O D S               |
    ============================================*/
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername();
    // Null username is invalid
    if (username == null) {
        throw new AccountException("Null usernames are not allowed by this realm.");
    }
    Connection conn = null;
    SimpleAuthenticationInfo info = null;
    try {
        conn = dataSource.getConnection();
        String password = null;
        String salt = null;
        switch(saltStyle) {
            case NO_SALT:
                password = getPasswordForUser(conn, username)[0];
                break;
            case CRYPT:
                // TODO: separate password and hash from getPasswordForUser[0]
                throw new ConfigurationException("Not implemented yet");
            // break;
            case COLUMN:
                String[] queryResults = getPasswordForUser(conn, username);
                password = queryResults[0];
                salt = queryResults[1];
                break;
            case EXTERNAL:
                password = getPasswordForUser(conn, username)[0];
                salt = getSaltForUser(username);
        }
        if (password == null) {
            throw new UnknownAccountException("No account found for user [" + username + "]");
        }
        info = new SimpleAuthenticationInfo(username, password.toCharArray(), getName());
        if (salt != null) {
            info.setCredentialsSalt(ByteSource.Util.bytes(salt));
        }
    } catch (SQLException e) {
        final String message = "There was a SQL error while authenticating user [" + username + "]";
        if (log.isErrorEnabled()) {
            log.error(message, e);
        }
        // Rethrow any SQL errors as an authentication exception
        throw new AuthenticationException(message, e);
    } finally {
        JdbcUtils.closeConnection(conn);
    }
    return info;
}
Also used : AccountException(org.apache.shiro.authc.AccountException) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) ConfigurationException(org.apache.shiro.config.ConfigurationException) SQLException(java.sql.SQLException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) Connection(java.sql.Connection) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 9 with SimpleAuthenticationInfo

use of org.apache.shiro.authc.SimpleAuthenticationInfo in project shiro by apache.

the class AbstractHashedCredentialsMatcherTest method testBasic.

@Test
public void testBasic() {
    CredentialsMatcher matcher = (CredentialsMatcher) ClassUtils.newInstance(getMatcherClass());
    byte[] hashed = hash("password").getBytes();
    AuthenticationInfo account = new SimpleAuthenticationInfo("username", hashed, "realmName");
    AuthenticationToken token = new UsernamePasswordToken("username", "password");
    assertTrue(matcher.doCredentialsMatch(token, account));
}
Also used : AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) AuthenticationInfo(org.apache.shiro.authc.AuthenticationInfo) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) Test(org.junit.Test)

Example 10 with SimpleAuthenticationInfo

use of org.apache.shiro.authc.SimpleAuthenticationInfo in project shiro by apache.

the class CookieRememberMeManagerTest method onSuccessfulLogin.

@Test
public void onSuccessfulLogin() {
    HttpServletRequest mockRequest = createNiceMock(HttpServletRequest.class);
    HttpServletResponse mockResponse = createNiceMock(HttpServletResponse.class);
    WebSubject mockSubject = createNiceMock(WebSubject.class);
    expect(mockSubject.getServletRequest()).andReturn(mockRequest).anyTimes();
    expect(mockSubject.getServletResponse()).andReturn(mockResponse).anyTimes();
    CookieRememberMeManager mgr = new CookieRememberMeManager();
    org.apache.shiro.web.servlet.Cookie cookie = createMock(org.apache.shiro.web.servlet.Cookie.class);
    mgr.setCookie(cookie);
    // first remove any previous cookie
    cookie.removeFrom(isA(HttpServletRequest.class), isA(HttpServletResponse.class));
    // then ensure a new cookie is created by reading the template's attributes:
    expect(cookie.getName()).andReturn("rememberMe");
    expect(cookie.getValue()).andReturn(null);
    expect(cookie.getComment()).andReturn(null);
    expect(cookie.getDomain()).andReturn(null);
    expect(cookie.getPath()).andReturn(null);
    expect(cookie.getMaxAge()).andReturn(SimpleCookie.DEFAULT_MAX_AGE);
    expect(cookie.getVersion()).andReturn(SimpleCookie.DEFAULT_VERSION);
    expect(cookie.isSecure()).andReturn(false);
    expect(cookie.isHttpOnly()).andReturn(true);
    UsernamePasswordToken token = new UsernamePasswordToken("user", "secret");
    token.setRememberMe(true);
    AuthenticationInfo account = new SimpleAuthenticationInfo("user", "secret", "test");
    replay(mockSubject);
    replay(mockRequest);
    replay(cookie);
    mgr.onSuccessfulLogin(mockSubject, token, account);
    verify(mockRequest);
    verify(mockSubject);
    verify(cookie);
}
Also used : ShiroHttpServletRequest(org.apache.shiro.web.servlet.ShiroHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) WebSubject(org.apache.shiro.web.subject.WebSubject) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthenticationInfo(org.apache.shiro.authc.AuthenticationInfo) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) Test(org.junit.Test)

Aggregations

SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)39 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)15 AuthenticationException (org.apache.shiro.authc.AuthenticationException)12 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)9 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)5 AccountException (org.apache.shiro.authc.AccountException)4 Hash (org.apache.shiro.crypto.hash.Hash)4 Test (org.junit.Test)4 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)3 LockedAccountException (org.apache.shiro.authc.LockedAccountException)3 UnknownAccountException (org.apache.shiro.authc.UnknownAccountException)3 HashRequest (org.apache.shiro.crypto.hash.HashRequest)3 PAM (org.jvnet.libpam.PAM)3 PAMException (org.jvnet.libpam.PAMException)3 UnixUser (org.jvnet.libpam.UnixUser)3 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)2 ByteSource (org.apache.shiro.util.ByteSource)2 UserDO (cn.dubidubi.model.base.UserDO)1 TbUser (cn.exrick.manager.pojo.TbUser)1 PmphUser (com.bc.pmpheep.back.po.PmphUser)1