依赖引入:
<!--freemarker对shiro标签的支持--> <dependency> <groupId>net.mingsoft</groupId> <artifactId>shiro-freemarker-tags</artifactId> <version>1.0.0</version> </dependency>
配置:
import com.jagregory.shiro.freemarker.ShiroTags; import com.screenshot.autotestplatform.enums.DeleteFlagEnums; import com.screenshot.autotestplatform.service.VendorService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import javax.annotation.PostConstruct; /** * @author 高亚轩 * @date 2018/11/6 0006 下午 4:09 */ @Slf4j @Configuration public class FreeMarkerConfig { private final FreeMarkerConfigurer freeMarkerConfigurer; @Autowired public FreeMarkerConfig(FreeMarkerConfigurer freeMarkerConfigurer) { this.freeMarkerConfigurer = freeMarkerConfigurer; } @PostConstruct public void setSharedVariable() { freeMarkerConfigurer.getConfiguration().setSharedVariable("shiro", new ShiroTags()); } }
guest(游客)
<@shiro.guest> 您当前是游客,<a href="javascript:void(0);" class="dropdown-toggle qqlogin" >登录</a> </@shiro.guest>
user(已经登录,或者记住我登录)
<@shiro.user> 欢迎[<@shiro.principal/>]登录,<a href="/logout.shtml">退出</a> </@shiro.user>
authenticated(已经认证,排除记住我登录的)
<@shiro.authenticated> 用户[<@shiro.principal/>]已身份验证通过 </@shiro.authenticated>
notAuthenticated(和authenticated相反)
<@shiro.notAuthenticated> 当前身份未认证(包括记住我登录的) </@shiro.notAuthenticated>
这个功能主要用途,识别是不是本次操作登录过的,比如支付系统,进入系统可以用记住我的登录信息,但是当要关键操作的时候,需要进行认证识别。
principal标签
principal标签,取值取的是你登录的时候。在Realm实现类中的如下代码:
.... return new SimpleAuthenticationInfo(user,user.getPswd(), getName());
在new SimpleAuthenticationInfo(第一个参数,....)的第一个参数放的如果是一个username,那么就可以直接用。
<!--取到username--> <@shiro. principal/>
如果第一个参数放的是对象,比如我喜欢放User对象。那么如果要取username字段。
<!--需要指定property--> <@shiro.principal property="username"/>
和Java如下Java代码一致
User user = (User)SecurityUtils.getSubject().getPrincipals(); String username = user.getUsername();
hasRole标签(判断是否拥有这个角色)
<@shiro.hasRole name="admin"> 用户[<@shiro.principal/>]拥有角色admin<br/> </@shiro.hasRole>
hasAnyRoles标签(判断是否拥有这些角色的其中一个)
<@shiro.hasAnyRoles name="admin,user,member"> 用户[<@shiro.principal/>]拥有角色admin或user或member<br/> </@shiro.hasAnyRoles>
lacksRole标签(判断是否不拥有这个角色)
<@shiro.lacksRole name="admin"> 用户[<@shiro.principal/>]不拥有admin角色 </@shiro.lacksRole>
hasPermission标签(判断是否有拥有这个权限)
<@shiro.hasPermission name="user:add"> 用户[<@shiro.principal/>]拥有user:add权限 </@shiro.hasPermission>
lacksPermission标签(判断是否没有这个权限)
<@shiro.lacksPermission name="user:add"> 用户[<@shiro.principal/>]不拥有user:add权限 </@shiro.lacksPermission>
还没有评论,来说两句吧...