Search in sources :

Example 1 with SysUser

use of com.goku.demo.entity.SysUser in project springBoot-learn-demo by nbfujx.

the class ShiroRealm method doGetAuthenticationInfo.

/**
 * 登录验证
 * *
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
    String userName = token.getUsername();
    SysUser user = sysUserService.getSysUserByUsername(token.getUsername());
    if (user != null) {
        // 设置用户session
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("user", user);
        return new SimpleAuthenticationInfo(userName, user.getPassword(), getName());
    } else {
        return null;
    }
}
Also used : SysUser(com.goku.demo.entity.SysUser) Session(org.apache.shiro.session.Session)

Example 2 with SysUser

use of com.goku.demo.entity.SysUser in project springBoot-learn-demo by nbfujx.

the class ShiroRealm method doGetAuthorizationInfo.

/**
 *权限验证
 * *
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    SysUser user = sysUserService.getSysUserByUsername((String) principalCollection.getPrimaryPrincipal());
    // 把principals放session中 key=userId value=principals
    SecurityUtils.getSubject().getSession().setAttribute(String.valueOf(user.getId()), SecurityUtils.getSubject().getPrincipals());
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    Set<UserRolePO> urpos = sysUserService.listUserRolePO(user);
    Set<UserPermissionPO> uppos = sysUserService.listUserPermissionPO(user);
    // 赋予角色
    for (UserRolePO userRole : urpos) {
        this.logger.info(userRole.getRoleId());
        info.addRole(userRole.getRoleId());
    }
    // 赋予权限
    for (UserPermissionPO userPermission : uppos) {
        this.logger.info(userPermission.getPermission());
        info.addStringPermission(userPermission.getPermission());
    }
    return info;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) SysUser(com.goku.demo.entity.SysUser) UserPermissionPO(com.goku.demo.common.po.UserPermissionPO) UserRolePO(com.goku.demo.common.po.UserRolePO)

Example 3 with SysUser

use of com.goku.demo.entity.SysUser in project springBoot-learn-demo by nbfujx.

the class SysUserExtRepositoryImpl method getSysUserWithRoleAndMenu.

@Override
public SysUser getSysUserWithRoleAndMenu(String userid) {
    String sql = "SELECT u FROM SysUser u " + "left join fetch u.sysRoles s left join fetch s.sysMenus rm " + "left join fetch u.sysMenus m where u.id=:userid";
    Query query = entityManager.createQuery(sql).setParameter("userid", userid);
    SysUser sysUser = (SysUser) query.getSingleResult();
    return sysUser;
}
Also used : Query(javax.persistence.Query) SQLQuery(org.hibernate.SQLQuery) SysUser(com.goku.demo.entity.SysUser)

Example 4 with SysUser

use of com.goku.demo.entity.SysUser in project springBoot-learn-demo by nbfujx.

the class TestUserRepositroy method getSysUserInfo.

@Test
public void getSysUserInfo() {
    SysUser sysUser = sysUserService.getSysUser("999");
    System.out.print(JSON.toJSONString(sysUserService.listUserPermissionPO(sysUser)));
    System.out.print(JSON.toJSONString(sysUserService.listUserRolePO(sysUser)));
}
Also used : SysUser(com.goku.demo.entity.SysUser) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

SysUser (com.goku.demo.entity.SysUser)4 UserPermissionPO (com.goku.demo.common.po.UserPermissionPO)1 UserRolePO (com.goku.demo.common.po.UserRolePO)1 Query (javax.persistence.Query)1 SimpleAuthorizationInfo (org.apache.shiro.authz.SimpleAuthorizationInfo)1 Session (org.apache.shiro.session.Session)1 SQLQuery (org.hibernate.SQLQuery)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1