总结之shiro(五)——过滤器、注解式、jsp标签控制

过滤器

过滤器简称对应的java类
anonorg.apache.shiro.web.filter.authc.AnonymousFilter
authcorg.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
permsorg.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
portorg.apache.shiro.web.filter.authz.PortFilter
restorg.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
rolesorg.apache.shiro.web.filter.authz.RolesAuthorizationFilter
sslorg.apache.shiro.web.filter.authz.SslFilter
userorg.apache.shiro.web.filter.authc.UserFilter
logoutorg.apache.shiro.web.filter.authc.LogoutFilter

在spring-shiro.xml配置过滤器

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <!-- loginUrl认证地址,如果没有认证将会此地址进行认证-->
        <property name="loginUrl" value="/toLogin"/>
        <!-- 认证成功统一跳转到index,建议不配置,shiro认证成功自动跳请求路径-->
        <!--<property name="successUrl" value="index"/>-->
        <!-- 通过unauthorizedUrl指定没有权限操作时跳转页面-->
        <property name="unauthorizedUrl" value="403.html" />
        <property name="filterChainDefinitions">
            <value>
                <!-- 这里顺序执行 anon不要认证 authc需要认证,一般情况下 /*放在最下面-->
                <!--加载静态资源 /js/** = anon-->
                /login.html = anon
                /toLogin = anon
                /testRole2=roles["admin"]
                /testRole3=rolesOr["admin","admin1"]
                /testPerms =perms[user:select]
                /testPerms1 =perms["user:select","user:update"]
                <!-- 请求logout地址(这个地址可以不存在),shiro去清除session-->
                <!--/logout = logout-->
                /** = authc
            </value>
        </property>
        <property name="filters">
            <util:map>
                <entry key="rolesOr" value-ref="rolesOrFilter"/>
            </util:map>
        </property>
    </bean>

在过滤器中配路径,路径多会造成过滤器配置太繁琐了,可以使用注解

注解

对controller开启AOP
在springmvc.xml中配置shiro注解支持,可在controller方法中使用shiro注解配置权限

	<aop:config proxy-target-class="true"/>
    <bean class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>

权限注解控制

  • @RequiresPermissions("")//权限
  • @RequiresRoles()//角色
  	@RequestMapping(value = "/massages/{id}")
    @RequiresPermissions("massage:select")//该方法必须有massage:select权限才能访问
    public String findMassageById(@PathVariable Integer id,ModelMap modelMap){

jsp标签控制

Jsp页面添加:
<%@ tagliburi=“http://shiro.apache.org/tags” prefix=“shiro” %>

标签名称标签条件(均是显示标签内容)
<shiro:authenticated >登录之后
<shiro:notAuthenticated >用户在没有RememberMe时
<shiro:user >用户在RememberMe时
<shiro:hasAnyRoles name=“abc,123” >在有abc或者123角色时
<shiro:hasRole name=“abc”>拥有角色abc
<shiro:lacksRole name=“abc”>没有角色abc
<shiro:hasPermission name=“abc”>拥有权限资源abc
<shiro:lacksPermission name=“abc”>没有abc权限资源
<shiro:principal >显示用户身份名称
<shiro:principal property=“username”/>显示用户身份中的属性值

jsp页面添加标签

例:没有认证——出现注册、登录按钮

<shiro:notAuthenticated>
        <button >注册</button>
        <button >登录</button>
</shiro:notAuthenticated>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值