1. 程式人生 > 其它 >SpringBoot整合shiro環境搭建

SpringBoot整合shiro環境搭建


1)在原來的專案基礎上,新建一個springboot模組


2)匯入thymeleaf依賴:


<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3)測試保證環境沒問題:

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
</head>
<body>

<h1>首頁</h1>

<p th:text="${msg}"></p>

</body>
</html>

MyController:

@Controller
public class MyController {

    //跳轉到首頁
    @RequestMapping({"/", "/index","index.html"})
    public String toIndex(Model model){
        model.addAttribute("msg","Hello!Shiro!");
        return "index";
    }

}

4)整合shiro


  • 1.匯入shiro整合spring的包
        <!--shiro整合spring的包-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.8.0</version>
        </dependency>

  • 2.目錄

  • 3.配置Shiro

ShiroConfig:

@Configuration
public class ShiroConfig {

    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("dwsm") DefaultWebSecurityManager sm){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //設定安全管理器
        bean.setSecurityManager(sm);
        return bean;
    }


    //DefaultWebSecurityManager
    //@Qualifier("userRealm") UserRealm userRealm這裡的意思是:使得這裡的userRealm與spring容器中的beanid="userRealm"的bean物件繫結
    @Bean(name = "dwsm") //這裡加上(name = "dwsm")之後,元件名就由原來的getDefaultWebSecurityManager變為dwsm
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //關聯UserRealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //建立 realm 物件  需要自定義一個類UserRealm
    //這裡的@Bean是給spring容器新增元件,並且返回型別UserRealm即為元件型別,該元件的id=“userRealm”即方法名
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

}


自定義一個UserRealm繼承AuthorizingRealm

//自定義的realm
public class UserRealm extends AuthorizingRealm {

    //授權
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("執行了授權doGetAuthorizationInf");
        return null;
    }

    //認證
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("執行了認證doGetAuthorizationInf");
        return null;
    }
}


  • 4.前端頁面

add.html:

<h1>add</h1>

update.html:

<h1>update</h1>

index.html:

<h1>首頁</h1>

<p th:text="${msg}"></p>

<a th:href="@{/user/add}">add</a>
<a th:href="@{/user/update}">update</a>

  • 5.MyController:

@Controller
public class MyController {

    //跳轉到首頁
    @RequestMapping({"/", "/index","index.html"})
    public String toIndex(Model model){
        model.addAttribute("msg","Hello!Shiro!");
        return "index";
    }

    @RequestMapping("/user/add")
    public String toAdd() {
        return "user/add";
    }


    @RequestMapping("/user/update")
    public String toUpdate() {
        return "user/update";
    }

}