Inherit the FreeMarkerConfigurer class and override the afterPropertiesSet() method;
import com.jagregory.shiro.freemarker.ShiroTags;
import freemarker.template.TemplateException;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.io.IOException;
/**
* 继承FreeMarkerConfigurer类,重写afterPropertiesSet()方法;
* 集成shiroTags标签
* Created by zsc on 2016/1/5.
*/
public class ShiroTagFreeMarkerConfigurer extends FreeMarkerConfigurer {
@Override
public void afterPropertiesSet() throws IOException, TemplateException {
super.afterPropertiesSet();
this.getConfiguration().setSharedVariable("shiro", new ShiroTags());
}
}
<bean
class="com.shiro.shiro.ShiroTagFreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/page/" />
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8</prop>
</props>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" Change this class to
ShiroTagFreeMarkerConfigurer
This class will do
Then call directly in the foreground:
guest tag: verify whether the current user is a "guest", that is, an unauthenticated (including unremembered) user; shiro tag: <shiro:guest></shiro:guest>; in freemark: <@shiro.guest> </ @shiro.guest>
user tag: authenticated or remembered user shiro tag: <shiro:user> </shiro:user> ; in freemark: <@shiro.user> </@shiro.user>
authenticated tag: An authenticated user. Remembered users are not included, which is the difference from the user tag. shiro tag: <shiro:authenticated> </shiro:authenticated>; in freemark: <@shiro.authenticated></@shiro.authenticated>
notAuthenticated tag: unauthenticated users. Opposite of authenticated tag. shiro tag: <shiro:notAuthenticated> </shiro:notAuthenticated>; in freemark: <@shiro.notAuthenticated></@shiro.notAuthenticated>
principal tag: output current user information, usually login account information shiro tag: Hello, < @shiro.principal property="name" /> ; in freemarker: Hello, <@shiro.
hasRole tag: Verify whether the current user belongs to this role, shiro tag: <shiro:hasRole name="administrator"> Administer the system </shiro:hasRole> ; in freemarker: <@shiro.hasRole name=”admin”>Hello admin !</@shiro.hasRole>
hasAnyRoles tag: Verify that the current user belongs to any of these roles, separated by commas, shiro tags: <shiro:hasAnyRoles name="admin,user,operator"> Administer the system < /shiro:hasAnyRoles> ; in freemarker: <@shiro.hasAnyRoles name="admin,user,operator">Hello admin!</@shiro.hasAnyRoles>
hasPermission tag: verify whether the current user has this permission, shiro tag: <shiro :hasPermission name="/order:*"> Order</shiro:hasPermission> ; in freemarker: <@shiro.hasPermission name="/order:*">Order/@shiro.hasPermission>
lacksRole tag: Verify that the current user does not Belong to this role, contrary to the hasRole tag, the shiro tag: <shiro:hasRole name=" admin"> Administer the system </shiro:hasRole> ;freemarker中:<@shiro.hasRole name="admin">Hello admin!</@shiro.hasRole>
lacksPermission tag: Verify that the current user does not have a certain permission, which is relative to the hasPermission tag, shiro tag: <shiro:lacksPermission name="/order:*"> trade </shiro:lacksPermission> ; in freemarker: <@shiro. lacksPermission name="/order:*">trade</@shiro.lacksPermission>