1. 程式人生 > >Spring Boot shiro 許可權管理 小例子

Spring Boot shiro 許可權管理 小例子

1.引入基本依賴

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

 

  <

groupId>com.kfit</groupId>

  <artifactId>spring-boot-shiro1</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>spring-boot-shiro1</name>

  <url

>http://maven.apache.org</url>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <java.version>1.8</java.version>

  </properties>

   

    <!--

       spring boot 父節點依賴,

       引入這個之後相關的引入就不需要新增version配置,

       spring boot會自動選擇最合適的版本進行新增。

     -->

    <parent>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-parent</artifactId>

       <version>1.3.3.RELEASE</version>

    </parent>    

 

  <dependencies>

   

        <!-- spring boot web支援:mvc,aop... -->

       <dependency>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-web</artifactId>

       </dependency>

      

       <!-- thmleaf模板依賴. -->

       <dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-thymeleaf</artifactId>

       </dependency>

   

  </dependencies>

</project>

2.

編寫網頁檔案:

index.html, login.html,   userInfo.html,  userInfoAdd.html

index.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Insert title here</title>

</head>

<body>

    <h3>index</h3>

</body>

</html>

 

login.html :

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Insert title here</title>

</head>

<body>

            錯誤資訊:<h4 th:text="${msg}"></h4>

       <form action="" method="post">

           <p>賬號:<input type="text" name="username" value="admin"/></p>

           <p>密碼:<input type="text" name="password" value="123456"/></p>

           <p><input type="submit" value="登入"/></p>

       </form>

</body>

</html>


 

(e)編寫HomeController

com.kfit.root.controller新建HomeController類:

package com.kfit.root.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

 

@Controller

publicclass HomeController {

   

    @RequestMapping({"/","/index"})

    public String index(){

       return"/index";

    }

   

    @RequestMapping(value="/login",method=RequestMethod.GET)

    public String login(){

       return"login";

    }

   

}




結果如圖