最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot+thymeleaf+shiro標簽的實例

 更新時間:2022年01月25日 10:46:15   作者:占星安啦  
這篇文章主要介紹了springboot+thymeleaf+shiro標簽的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

1、pom中加入依賴

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
			<version>1.5.6.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
		<dependency>
			<groupId>org.thymeleaf</groupId>
			<artifactId>thymeleaf</artifactId>
			<version>${thymeleaf.version}</version>
		</dependency>
        <!-- shiro安全框架 -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring</artifactId>
			<version>1.4.0</version>
		</dependency>
		<!--thymeleaf-shiro-extras-->
		<dependency>
			<groupId>com.github.theborakompanioni</groupId>
			<artifactId>thymeleaf-extras-shiro</artifactId>
			<version>1.2.1</version>
		</dependency>

2、用戶-角色-權(quán)限的表關(guān)系

//用戶表
public class User {
    private Integer userId;
    private String userName;
    private Set<Role> roles = new HashSet<>();
}
//角色表
public class User {
    private Integer id;
    private String role;
    private Set<Module> modules = new HashSet<>();
    private Set<User> users = new HashSet<>();
}
//權(quán)限表
public class Module {
    private Integer mid;
    private String mname;
    private Set<Role> roles = new HashSet<>();
}
    
    
//用戶查詢
<resultMap id="BaseResultMap" type="com.lanyu.common.model.User" >
    <id column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <!-- 多對多關(guān)聯(lián)映射:collection -->
    <collection property="roles" ofType="Role">
      <id property="id" column="c_id" />
      <result property="role" column="role" />
      <collection property="modules" ofType="Module">
        <id property="mid" column="mid"/>
        <result property="mname" column="mname"/>
      </collection>
    </collection>
  </resultMap>
  
//查詢用戶信息,返回結(jié)果會自動分組,得到用戶信息
  <select id="selectByPhone" resultMap="BaseResultMap" parameterType="java.lang.String" >
    SELECT
	  u.*, r.*, m.*
    FROM
        sys_user u
    INNER JOIN sys_user_role ur ON ur.userId = u.user_id
    INNER JOIN sys_role r ON r.rid = ur.roleid
    INNER JOIN sys_role_module mr ON mr.rid = r.rid
    INNER JOIN sys_module m ON mr.mid = m.mid
    WHERE
	  u.user_name=#{username} or u.phone=#{username};
  </select>

3、編寫shiro核心類

@Configuration
public class ShiroConfiguration {
    
    //用于thymeleaf模板使用shiro標簽
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }
    @Bean(name="shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager manager) {
        ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
        bean.setSecurityManager(manager);
        //配置登錄的url和登錄成功的url
        bean.setLoginUrl("/loginpage");
        bean.setSuccessUrl("/indexpage");
        //配置訪問權(quán)限
        LinkedHashMap<String, String> filterChainDefinitionMap=new LinkedHashMap<>();
//        filterChainDefinitionMap.put("/loginpage*", "anon"); //表示可以匿名訪問
        filterChainDefinitionMap.put("/admin/*", "authc");//表示需要認證才可以訪問
        filterChainDefinitionMap.put("/logout*","anon");
        filterChainDefinitionMap.put("/img/**","anon");
        filterChainDefinitionMap.put("/js/**","anon");
        filterChainDefinitionMap.put("/css/**","anon");
        filterChainDefinitionMap.put("/fomts/**","anon");
        filterChainDefinitionMap.put("/**", "anon");
        bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return bean;
    }
    //配置核心安全事務(wù)管理器
    @Bean(name="securityManager")
    public SecurityManager securityManager(@Qualifier("authRealm") AuthRealm authRealm) {
        System.err.println("--------------shiro已經(jīng)加載----------------");
        DefaultWebSecurityManager manager=new DefaultWebSecurityManager();
        manager.setRealm(authRealm);
        return manager;
    }
    //配置自定義的權(quán)限登錄器
    @Bean(name="authRealm")
    public AuthRealm authRealm(@Qualifier("credentialsMatcher") CredentialsMatcher matcher) {
        AuthRealm authRealm=new AuthRealm();
        authRealm.setCredentialsMatcher(matcher);
        return authRealm;
    }
    //配置自定義的密碼比較器
    @Bean(name="credentialsMatcher")
    public CredentialsMatcher credentialsMatcher() {
        return new CredentialsMatcher();
    }
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator creator=new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);
        return creator;
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager manager) {
        AuthorizationAttributeSourceAdvisor advisor=new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(manager);
        return advisor;
    }
}
— - - -- - -- - -- - -- - - -- - - - -- 
public class AuthRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    //認證.登錄
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken utoken=(UsernamePasswordToken) token;//獲取用戶輸入的token
        String username = utoken.getUsername();
        User user = userService.selectByPhone(username);
        return new SimpleAuthenticationInfo(user, user.getPassword(),this.getClass().getName());//放入shiro.調(diào)用CredentialsMatcher檢驗密碼
    }
    //授權(quán)
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        User user=(User) principal.fromRealm(this.getClass().getName()).iterator().next();//獲取session中的用戶
        List<String> permissions=new ArrayList<>();
        Set<Role> roles = user.getRoleList();
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        List<String> listrole = new ArrayList<>();
        if(roles.size()>0) {
            for(Role role : roles) {
                if(!listrole.contains(role.getRole())){
                    listrole.add(role.getRole());
                }
                Set<Module> modules = role.getModules();
                if(modules.size()>0) {
                    for(Module module : modules) {
                        permissions.add(module.getMname());
                    }
                }
            }
        }
        info.addRoles(listrole);                       //將角色放入shiro中.
    info.addStringPermissions(permissions);         //將權(quán)限放入shiro中.
        return info;
    }
}
//自定義密碼比較器
public class CredentialsMatcher extends SimpleCredentialsMatcher {
    private  Logger logger = Logger.getLogger(CredentialsMatcher.class);
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken utoken=(UsernamePasswordToken) token;
        //所需加密的參數(shù)  即  用戶輸入的密碼
        String source = String.valueOf(utoken.getPassword());
        //[鹽] 一般為用戶名 或 隨機數(shù)
        String salt = utoken.getUsername();
        //加密次數(shù)
        int hashIterations = 50;
        SimpleHash sh = new SimpleHash("md5", source, salt, hashIterations);
        String Strsh =sh.toHex();
        //打印最終結(jié)果
        logger.info("正確密碼為:"+Strsh);
        //獲得數(shù)據(jù)庫中的密碼
        String dbPassword= (String) getCredentials(info);
        logger.info("數(shù)據(jù)庫密碼為:"+dbPassword);
        //進行密碼的比對
        return this.equals(Strsh, dbPassword);
    }
}

4、登錄控制器

    @RequestMapping("/loginUser")
    public String loginUser(String username,String password,HttpSession session) {
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(username,password);
        Subject subject = SecurityUtils.getSubject();
        Map map=new HashMap();
        try {
            subject.login(usernamePasswordToken);   //完成登錄
            User user=(User) subject.getPrincipal();
            session.setAttribute("user", user);
            return "index";
        } catch (IncorrectCredentialsException e) {
            map.put("msg", "密碼錯誤");
        } catch (LockedAccountException e) {
            map.put("msg", "登錄失敗,該用戶已被凍結(jié)");
        } catch (AuthenticationException e) {
            map.put("msg", "該用戶不存在");
        } catch (Exception e) {
            return "login";//返回登錄頁面
        }
        return map.toString();
    }

5、thymeleaf頁面權(quán)限控制

<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
	  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
	  
//作為屬性控制
<button  type="button" shiro:authenticated="true" class="btn btn-outline btn-default">
	<i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
</button>
//作為標簽
<shiro:hasRole name="admin">
	<button type="button" class="btn btn-outline btn-default">
		<i class="glyphicon glyphicon-heart" aria-hidden="true"></i>
	</button>
</shiro:hasRole>

6、標簽說明

guest標簽
  <shiro:guest>
  </shiro:guest>
  用戶沒有身份驗證時顯示相應(yīng)信息,即游客訪問信息。
user標簽
  <shiro:user>  
  </shiro:user>
  用戶已經(jīng)身份驗證/記住我登錄后顯示相應(yīng)的信息。
authenticated標簽
  <shiro:authenticated>  
  </shiro:authenticated>
  用戶已經(jīng)身份驗證通過,即Subject.login登錄成功,不是記住我登錄的。
notAuthenticated標簽
  <shiro:notAuthenticated>
  
  </shiro:notAuthenticated>
  用戶已經(jīng)身份驗證通過,即沒有調(diào)用Subject.login進行登錄,包括記住我自動登錄的也屬于未進行身份驗證。
principal標簽
  <shiro: principal/>
  
  <shiro:principal property="username"/>
  相當(dāng)于((User)Subject.getPrincipals()).getUsername()。
lacksPermission標簽
  <shiro:lacksPermission name="org:create">
 
  </shiro:lacksPermission>
  如果當(dāng)前Subject沒有權(quán)限將顯示body體內(nèi)容。
hasRole標簽
  <shiro:hasRole name="admin">  
  </shiro:hasRole>
  如果當(dāng)前Subject有角色將顯示body體內(nèi)容。
hasAnyRoles標簽
  <shiro:hasAnyRoles name="admin,user">
   
  </shiro:hasAnyRoles>
  如果當(dāng)前Subject有任意一個角色(或的關(guān)系)將顯示body體內(nèi)容。
lacksRole標簽
  <shiro:lacksRole name="abc">  
  </shiro:lacksRole>
  如果當(dāng)前Subject沒有角色將顯示body體內(nèi)容。
hasPermission標簽
  <shiro:hasPermission name="user:create">  
  </shiro:hasPermission>
  如果當(dāng)前Subject有權(quán)限將顯示body體內(nèi)容

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java自動添加重寫的toString方法詳解

    Java自動添加重寫的toString方法詳解

    在本篇文章里小編給大家整理了關(guān)于Java自動添加重寫的toString方法總結(jié),需要的朋友們學(xué)習(xí)下。
    2019-07-07
  • Java中JVM常用參數(shù)配置教程(提供配置示例)

    Java中JVM常用參數(shù)配置教程(提供配置示例)

    這篇文章主要給大家介紹了關(guān)于Java中JVM常用參數(shù)配置的相關(guān)資料, jvm的參數(shù)有很多,必須知道參數(shù)分類并且記住面試常見的幾個參數(shù),文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-06-06
  • java小程序之控制臺字符動畫的實現(xiàn)

    java小程序之控制臺字符動畫的實現(xiàn)

    這篇文章主要給大家介紹了java小程序之控制臺字符動畫實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Java方法參數(shù)傳遞如何實現(xiàn)

    Java方法參數(shù)傳遞如何實現(xiàn)

    這篇文章主要介紹了Java方法參數(shù)傳遞如何實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • 導(dǎo)入SpringCloud依賴踩的坑及解決

    導(dǎo)入SpringCloud依賴踩的坑及解決

    這篇文章主要介紹了導(dǎo)入SpringCloud依賴踩的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • SpringBoot攔截器的使用小結(jié)

    SpringBoot攔截器的使用小結(jié)

    今天給大家總結(jié)一下SpringBoot下攔截器的使用,需要的朋友參考下吧
    2017-05-05
  • Java處理日期時間的方法匯總

    Java處理日期時間的方法匯總

    這篇文章主要給大家介紹了利用Java中的Calendar 類處理日期時間的方法匯總,其中包括取日期的每部分、取當(dāng)月的第一天或最后一天、求兩個日期之間相隔的天數(shù)以及一年前的日期等等的示例代碼,有需要的朋友們可以直接參考借鑒,下面來一起看看吧。
    2016-12-12
  • SpringBoot項目接入MQTT的詳細指南

    SpringBoot項目接入MQTT的詳細指南

    MQTT是一種輕量級的消息傳輸協(xié)議,特別適用于物聯(lián)網(wǎng)(IoT)場景,具有低帶寬、高延遲網(wǎng)絡(luò)環(huán)境下的優(yōu)勢,SpringBoot作為流行的 Java開發(fā)框架,能夠方便地與MQTT集成,實現(xiàn)高效的消息通信,本文將詳細介紹如何在SpringBoot項目中接入MQTT,需要的朋友可以參考下
    2025-03-03
  • mybatis自動建表的實現(xiàn)方法

    mybatis自動建表的實現(xiàn)方法

    這篇文章主要介紹了mybatis自動建表的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • JPA如何將查詢結(jié)果轉(zhuǎn)換為DTO對象

    JPA如何將查詢結(jié)果轉(zhuǎn)換為DTO對象

    這篇文章主要介紹了JPA如何將查詢結(jié)果轉(zhuǎn)換為DTO對象,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評論

屯门区| 册亨县| 水富县| 原阳县| 多伦县| 杨浦区| 旬邑县| 甘洛县| 东乌| 漳浦县| 霸州市| 盱眙县| 和硕县| 阳新县| 磴口县| 大石桥市| 鲁甸县| 横山县| 高邑县| 盐山县| 古田县| 莲花县| 泰安市| 万盛区| 壤塘县| 巍山| 邯郸县| 开封市| 南乐县| 濮阳市| 苏尼特右旗| 咸阳市| 栾城县| 龙门县| 巩义市| 神池县| 永年县| 高要市| 安溪县| 汪清县| 财经|