Freemarker-shiro label

一、引入依赖(已解决版本冲突)
复制代码
<!-- shiro-freemarker-tags start -->
<dependency>
    <groupId>net.mingsoft</groupId>
    <artifactId>shiro-freemarker-tags</artifactId>
    <version>1.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-all</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- shiro-freemarker-tags end -->

<!-- freemarker start -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.28</version>
</dependency>
<!-- freemarker end -->

<!-- shiro begin -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-all</artifactId>
    <version>1.4.0</version>
</dependency>
<!-- shiro end -->
复制代码
二、配置
Java代码:

复制代码
public class FreeMarkerConfigExtend extends FreeMarkerConfigurer {
    @Override
    public void afterPropertiesSet() throws IOException, TemplateException {
        super.afterPropertiesSet ();
        // add tags shiro
        the Configuration this.getConfiguration CFG = (); 
        cfg.setSharedVariable ( "Shiro", new new ShiroTags ()); 
    } 
} 
copy the code 
copy the code 
<- - freemarker environment configuration!> 
<The bean ID = "freemarkerConfig" class = "COM .demo.shiro.common.freemarker.FreeMarkerConfigExtend "> 
    <-! template position, there would not configured with the following -> 
    <Property name =" templateLoaderPath "value =" / the WEB-INF / views "/> 
    < property name = "freemarkerSettings"> < -! some settings -> 
        <The props> 
            <prop Key = "template_update_delay"> 0 </ prop> 
            <prop Key = "default_encoding"> UTF-. 8 </ prop> 
            <prop key="locale">zh_CN</prop>
            <prop key="boolean_format">true,false</prop>
            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
            <prop key="date_format">yyyy-MM-dd</prop>
            <prop key="time_format">HH:mm:ss</prop>
            <prop key="number_format">0.##########</prop>
            <prop key="classic_compatible">true</prop>
            <prop key="template_exception_handler">ignore</prop>
            <prop key="auto_import">
                <!-- 自动装载,引入Freemarker,用于Freemarker Macro引入 -->
                /common/_meta.ftl as _meta,
                /common/_footer.ftl as _footer
                <!--/common/menu.ftl as _menu-->
            </prop>
        </props>
    </property>
</bean>
<@ shiro.guest>  
1. guest (Guest)
three, shiro Detailed tag
Copy the code
 
    You are a visitor, <a href="javascript:void(0);"> login </a> 
</ @ shiro.guest> 
2. the User (already logged in, login or remember me) 

<@ shiro.user>   
    Welcome [<@ shiro.principal />] Log in, <a href="/logout.shtml"> exit </a>   
</ @ shiro.user>    
3. the authenticated (already certified, excluding remember I live in log) 

<@ shiro.authenticated>   
    user [<@ shiro.principal />] has been authenticated by   
</ @ shiro.authenticated> 
4. notAuthenticated (and authenticated reverse) 

<@ shiro.notAuthenticated> 
    current identity unauthenticated (including Remember my login) 
</ @ shiro.notAuthenticated> 
the function main purposes: to identify this operation is not logged, such as payment systems, access to the system can remember my login information, but when you want key operation when authentication is required to identify. 

5. 

principal labels principal label, the value is taken when you log in. In the implementation class Realm following code: 

...
return new SimpleAuthenticationInfo (user, user.getPswd ( 
<@ shiro.hasAnyRoles name = "admin,   user,
In the new SimpleAuthenticationInfo (the first argument, ....) The first argument is put If a username, then it can be used directly. 

<! - get the username -> 
<@shiro Principal /.> 
If the first argument is put objects such put User object. So if you want to take the username field. 

<! - specify Property -> 
<Property @ shiro.principal = "username" /> 
and the following Java Java codes match 

the User User = (the User) SecurityUtils.getSubject () the getPrincipals ();. 
String username = user.getUsername (); 
6. the label the hasRole (determines whether it has this role) 

<shiro.hasRole @ name = "ADMIN">   
    user [<@ shiro.principal />] has a role ADMIN   
</ @ shiro.hasRole>    
. 7 . hasAnyRoles tag (one of which determines whether to have those roles) 
. 8.

    User [<@ shiro.principal />] has a role or a user or admin a Member   
</ @ shiro.hasAnyRoles>   

<@ shiro.lacksRole name = "admin"   > 
    User [<@ shiro.principal />] does not have the admin role 
</ @ shiro.lacksRole>    
9. The tag the hasPermission (determined whether to have this privilege) 

<name @ shiro.hasPermission = "user / add">   
    user [<@ shiro.principal />] has user / add permissions 
</ @ shiro.hasPermission>    
10. the lacksPermission tag (this determines whether or not permission) 

<shiro.lacksPermission @ name = "user / add ">   
    user [<@ shiro.principal />] does not own user / add permissions 
</ @ shiro.lacksPermission>

  

Guess you like

Origin www.cnblogs.com/joelan0927/p/11374189.html