Shiro权限

1.导入shiro的jar包

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-all</artifactId>
    <version>1.2.3</version>
</dependency>

2.在登录的时候,就进行登录的认证

//new一个终端
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
Subject subject = SecurityUtils.getSubject();
//运行login之后,进入添加认证和获取所有权限
subject.login(token);

3.自己写一个类,实现认证抽象类AuthorizingRealm,并且重写两个方法

public class MyShiroRealm extends AuthorizingRealm{

    @Autowired
    private AccountService accountService;

    @Autowired
    private PermissionService permissionService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
        //根据用户名,获取当前用户所有权限pc.getPrimaryPrincipal().toString()就是获取当前用户名
        List<String> premissionList = permissionService.findPremissionByUsername(pc.getPrimaryPrincipal().toString());
        //创建一个可以装所有权限的集合,把获得的权限全部放进去以后直接查找就行了。
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        for (String premission : premissionList) {
            simpleAuthorizationInfo.addStringPermission(premission);
        }
        return simpleAuthorizationInfo;
    }

    //根据用户名,获取用户,并且把用户放入认证信息
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken ac) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) ac;
        String username = token.getUsername();
        char[] password = token.getPassword();
        User user = accountService.getUserByUsernamePassword(username,password);
        if (user != null) {
            //将对象放入认证信息
            return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),getName());
        }
        return null;
    }

}

4.为shiro写一个配置文件

 <!--注入实现认证抽象类的类的全路径-->  
    <bean id="myShiroRealm" class="cn.hzy.shiro.MyShiroRealm"></bean>

    <!--配置安全认证-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--注入实现类-->
        <property name="realm" ref="myShiroRealm"></property>
    </bean>       

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--安全认证  -->
        <property name="securityManager" ref="securityManager"></property>
        <!--检查是否登录,否返回登录入口  -->
        <property name="loginUrl" value="login.jsp"></property>
        <!--检查是否登录,是返回主页-->
        <property name="successUrl" value="/user/home"></property>
        <!--检查是否有权限,没有走下面这个路径 -->
        <property name="unauthorizedUrl" value="/user/noPermission"></property>

        <property name="filterChainDefinitions">
            <value>
                <!--路径名     authc:验证是否认证,perms[xx]xx权限  需要xx权限名和路径名一样才是有权限-->
                /user/toLogin = authc,perms[/user/toLogin]
                /user/home = authc,perms[/user/home]
                /user/allUser = authc,perms[/user/allUser]
                /user/delUser = authc,perms[/user/delUser]
            </value>
        </property>
    </bean>

5.配置web.xml文件

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring*.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

6.对密码进行MD5加密

主要使用在注册,登录的时候我们把明文密码进行加密变成一个MD5加密后的暗码

//申明使用MD5加密
String hashAlgorithmName = "MD5";
//把用户名作为盐值使密码更加复杂。
//盐值是什么,比如我的密码是123通过用户名作为盐值的话就是123/用户名这样使我的密码更加复杂,那么下次有人
密码也是123那和我的密码还是不一样。
ByteSource solt = ByteSource.Util.bytes(user.getUsername());
//加密次数我设置的是1024次
int hashIterations = 1024;
//加密,参数(加密方式,原码,盐值,加密次数)
Object result = new SimpleHash(hashAlgorithmName,token.getPassword(), solt, hashIterations);
//此时,result是一个加密后的密码。

7.shiro标签的使用

//添加头进jsp文件中去
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro"%>
<shiro:hasPermission name="xx">有xx权限才会显示</shiro:hasPermission>

猜你喜欢

转载自blog.csdn.net/young_____hu/article/details/80007636