在web項目中使用shiro(認證、授權)


一.在web項目中實現認證
第一步,在web項目中導入shiro依賴的包
第二步,在web.xml中聲明shiro攔截權限的過濾器
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!--保證該過濾器的生命周期和spring 工廠中shiro過濾器對象的生命周期一致-->
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<!--聲明該過濾器代理工廠類中的id為什么的shiro過濾器對象-->
<init-param>
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第三步,在spring的主配置文件中聲明shiro的配置信息
shiro的配置信息比較多,一般不和spring的主配置文件放到一起。一般都會單獨建立一個shiro和spring集成的配置文件。 
創建好該文件后,要在spring.xml中引入該文件:
在spring中自定義域對象: 
/**
* 實現自定義域
* Authorization 授權(權限校驗)
*
* Authentication 認證(登錄校驗)
*
*/
public class AuthRealm extends AuthorizingRealm {
@Autowired
private UserService userService;
//獲取授權信息
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
}
//獲取認證信息
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws
AuthenticationException {
//獲取用戶名 不過一般往shiro中放置用戶身份信息的時候,不直接放用戶名字符串,放用戶對象
String username = token.getPrincipal().toString();
//有了用戶名,要根據用戶名在數據庫中查詢用戶對象
Users user = userService.findByUsername(username);
//判斷如果用戶對象不存在,拋出UnknownAccountException
if(user==null){
throw new UnknownAccountException("用戶名不存在");
}
//封裝用戶的身份對象 返回這個身份對象
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user,user.getPassword(),"authRealm");
return info;
}
}
在shiro的主配置文件spring-shiro.xml中做配置: 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--創建自定義域對象-->
<bean id="authRealm" class="com.aaa.ssm.realm.AuthRealm"></bean>
<!--創建shiro的安全管理器對象-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--要聲明域,在域中讀取認證和授權的數據-->
<property name="realm" ref="authRealm"></property>
</bean>
<!--創建shiro的過濾器對象-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!--要注入安全管理器對象-->
<property name="securityManager" ref="securityManager"></property>
<!--配置登錄請求的路徑-->
<property name="loginUrl" value="/user/login.do"></property>
<!--配置shiro認證和授權的過濾器-->
<property name="filterChainDefinitions">
<value>
<!--對靜態資源不攔截-->
<!--anon指匿名訪問的過濾器,所有匿名用戶都可以訪問static下面的資源-->
/static/*=anon
/user/login.do=anon
/login.jsp=anon
<!--authc指必須經過認證(登錄過之后)才能訪問的請求 /*代表所有有一個斜杠的請求都要經過認證 -->
/*=authc
/*/*=authc
</value>
</property>
</bean>
</beans>
第四步,在控制器中使用shiro去做登錄認證
/**
* 用戶登錄的請求
* @param user
* @return
*/
@RequestMapping("/login")
public String login(Users user, Model model, HttpSession session){
//獲取用戶的主體對象
Subject subject = SecurityUtils.getSubject();
//封裝用戶名和密碼的認證信息對象
UsernamePasswordToken upt = new UsernamePasswordToken(user.getUsername(),user.getPassword());
//進行登錄認證
try {
subject.login(upt);
}catch (Exception e){
e.printStackTrace();
System.out.println("用戶名或者密碼錯誤");
return "redirect:/login.jsp";
}
return "index";
}
二.在web項目中實現授權
當前我們還沒有配置授權的信息,所以用戶登錄成功之后能訪問所有的請求。 
配置文件的方式 
xml的方式 
第一步,在shiro的主配置文件中配置授權信息
第二步,當前登錄用戶有哪些授權信息從自定義的realm中讀取 
注解的方式
第一步,開始spring的shiro的注解的支持 
注意,開啟注解的支持之前,應該保證項目中有spring 的aop的jar包的,aspectj等的jar包。 
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- aspectj相關jar包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
要在spring mvc的主配置文件中聲明shiro的注解的支持: 
第二步,在控制器的代碼中增加需要授權的注解 
第三步,同xml的方式的第二步
第四步,聲明spring mvc的統一異常處理 
因為如果用戶沒有權限,會跳轉到500界面,不友好,我們可以使用spring mvc統一異常處理機制,讓用戶跳轉到一個統一的界面。 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM