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

spring+shiro 整合實例代碼詳解

 更新時間:2018年10月25日 10:25:41   作者:木土mango  
本文通過實例代碼給大家介紹spring+shiro 整合的過程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

一、添加相關依賴

<dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-core</artifactId>
   <version>1.2.1</version>
  </dependency>
  <dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-web</artifactId>
   <version>1.2.1</version>
  </dependency>
  <dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-ehcache</artifactId>
   <version>1.2.1</version>
  </dependency>
  <dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-spring</artifactId>
   <version>1.2.1</version>
  </dependency> 
  <dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.2</version>
  </dependency>

二、編寫代碼

1、自定義realm

public class CommonRealm extends AuthorizingRealm {
 @Autowired
 private UserLoginService userLoginService;
 @Override
 public String getName() {
  return "CommonRealm";
 }
 //授權
 @Override
 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  String usernmae = (String) principals.getPrimaryPrincipal();
  List<String> permissions = new ArrayList<String>();
  if ("admin".equals(usernmae)) {
   permissions.add("admin:ee");
  }
  SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
  info.addStringPermissions(permissions);
  return info;
 }
 //身份認證
 @Override
 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  String username = (String) token.getPrincipal();
  User user = userLoginService.getUser(username);
  if (user == null) {
   return null;
  }
  SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, user.getPassword(), getName());
  return info;
 }
}

2、login controller

@Controller
public class UserAction {
 @Autowired
 private UserLoginService userLoginService;
 @RequestMapping("/login.do")
 public String userLogin(HttpServletRequest request, String username, String password) throws Exception {
  // 如果登陸失敗從request中獲取異常信息,shiroLoginFailure就是shiro異常類的全限定名
  String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
  if (exceptionClassName != null) {
   if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
    // 最終會拋給異常處理器
    throw new XXXException("用戶名不存在");
   } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
    throw new XXXException("用戶名/密碼錯誤");
   } else {
    throw new Exception();// 最終在異常處理器生成未知錯誤
   }
  }
  // 如果登錄成功的話不走此方法,shiro認證成功會自動跳轉到上一個請求路徑,配的的successUrl沒效果,后邊會說
  // 登陸失敗走此方法,捕獲異常,然后 return ~ ,還到login頁面
  return "login.jsp";
 }
}

3、檢測權限 controller

 //此方法為了驗證權限是否生效
 @RequestMapping("/findAll.do")
 @RequiresPermissions("admin:ee")
 public ModelAndView list(HttpServletRequest request){
  ....... 
 }

三、常見問題

因為有一些特別常見的問題,需要修改xml配置,所以現(xiàn)在先手問題,把xml配置放在后邊,直接就配置完善好的xml

問題一:登陸成功后shiro默認跳到上一次請求,沒有上一次請求默認跳到/  ,那我們就想控制調到自己定義的路徑咋辦呢?

解決方案:

步驟一:繼承FormAuthenticationFilter類,重寫onLoginSuccess方法,這里可以自定義路徑,因為這里自定義了成功跳轉的路徑,所以配置里的successUrl不用配置,賠了也沒效果。。

public class LoginSuccessToFilter extends FormAuthenticationFilter {
 @Override
 protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
  WebUtils.getAndClearSavedRequest(request);
  WebUtils.redirectToSavedRequest(request,response,"/findAll.do");
  return false;
 }
}

步驟二:

在shiro的xml配置文件中配置

 <bean id="myfilter" class="com.xxx.realm.LoginSuccessToFilter"></bean>

在 shiroFilter配置中引入,完整xml在后邊

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  <property name="filters">
   <map>
    <entry key="authc" value-ref="myfilter"></entry>
   </map>
  </property>
</bean>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  <property name="filters">
   <map>
    <entry key="authc" value-ref="myfilter"></entry>
   </map>
  </property>
</bean>

四、Xml配置

applicationContext-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" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  <!-- 管理器,必須設置 -->
  <property name="securityManager" ref="securityManager"/>
  <property name="filters">
   <map>
    <entry key="authc" value-ref="myfilter"></entry>
   </map>
  </property>
  <!-- 攔截到,跳轉到的地址,通過此地址去認證 -->
  <property name="loginUrl" value="/login.do"/>
  <!-- 認證成功統(tǒng)一跳轉到/admin/index.do,建議不配置,shiro認證成功自動到上一個請求路徑 -->
  <!--<property name="successUrl" value="/findAll.do"/>-->
  <!-- 通過unauthorizedUrl指定沒有權限操作時跳轉頁面 -->
  <property name="unauthorizedUrl" value="/refuse.jsp"/>
  <!-- 自定義filter,可用來更改默認的表單名稱配置 -->
  <!--<property name="filters">-->
  <!--<map>-->
  <!--&lt;!&ndash; 將自定義 的FormAuthenticationFilter注入shiroFilter中 &ndash;&gt;-->
  <!--<entry key="authc" value-ref="formAuthenticationFilter" />-->
  <!--</map>-->
  <!--</property>-->
  <property name="filterChainDefinitions">
   <value>
    <!-- 對靜態(tài)資源設置匿名訪問 -->
    /image/** = anon
    /css/** = anon
    /js/** = anon
    /logout.do = logout
    /** = authc
   </value>
  </property>
 </bean>
 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
 <!-- securityManager安全管理器 -->
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  <property name="realm" ref="customRealm"/>
  <!-- 注入緩存管理器 -->
  <!--<property name="cacheManager" ref="cacheManager" />-->
  <!-- 注入session管理器 -->
  <!-- <property name="sessionManager" ref="sessionManager" /> -->
  <!-- 記住我 -->
  <!--<property name="rememberMeManager" ref="rememberMeManager" />-->
 </bean>
 <!-- 自定義realm -->
 <bean id="customRealm" class="com.dhl.realm.CommonRealm"></bean>
 <bean id="myfilter" class="com.dhl.realm.LoginSuccessToFilter"></bean>
 <!-- 憑證匹配器 -->
 <!--<bean id="credentialsMatcher"-->
   <!--class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">-->
  <!--&lt;!&ndash; 選用MD5散列算法 &ndash;&gt;-->
  <!--<property name="hashAlgorithmName" value="md5"/>-->
  <!--&lt;!&ndash; 進行一次加密 &ndash;&gt;-->
  <!--<property name="hashIterations" value="1"/>-->
 <!--</bean>-->
</beans>

springmvc的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
 <context:component-scan base-package="com.dhl.controller"></context:component-scan>
 <mvc:annotation-driven></mvc:annotation-driven>
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
 <!-- 開啟shiro注解的配置移動到這兒 -->
 <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
  <property name="proxyTargetClass" value="true" />
 </bean>
 <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  <property name="securityManager" ref="securityManager"/>
 </bean>
</beans>

以上就是一個大概的整合和遇到的兩個問題,博主也是查閱了很多的博客得到的較優(yōu)答案,整理出來,已備后續(xù)參考,遇到一樣問題的同學可以看看

相關文章

  • JavaScript中的closest方法示例詳解

    JavaScript中的closest方法示例詳解

    這篇文章主要介紹了JavaScript中closest方法的相關資料,closest()是JavaScript中的一個非常實用的?DOM?方法,用于查找與當前元素匹配的最近的祖先元素,它支持?CSS?選擇器,可以應用于事件委托、動態(tài)內容處理等場景,需要的朋友可以參考下
    2025-02-02
  • spring boot 不連接數(shù)據(jù)庫啟動的解決

    spring boot 不連接數(shù)據(jù)庫啟動的解決

    這篇文章主要介紹了spring boot 不連接數(shù)據(jù)庫啟動的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 淺談spring-boot-rabbitmq動態(tài)管理的方法

    淺談spring-boot-rabbitmq動態(tài)管理的方法

    這篇文章主要介紹了淺談spring-boot-rabbitmq動態(tài)管理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 深入了解JAVA 軟引用

    深入了解JAVA 軟引用

    這篇文章主要介紹了JAVA 軟引用的相關資料,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-08-08
  • IDEA如何將String類型轉json格式

    IDEA如何將String類型轉json格式

    在Java中,字符串字面量中的轉義字符會被自動轉換,但通過網(wǎng)絡獲取的字符串可能不會自動轉換,為了解決IDEA無法識別JSON字符串的問題,可以在本地對字符串進行一次轉換,替換轉義字符,從而生成可以被IDEA識別的新JSON字符串
    2025-01-01
  • SpringMVC form標簽引入及使用方法

    SpringMVC form標簽引入及使用方法

    這篇文章主要介紹了SpringMVC form標簽引入及使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • Java Iterator接口實現(xiàn)代碼解析

    Java Iterator接口實現(xiàn)代碼解析

    這篇文章主要介紹了Java Iterator接口實現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Yml轉properties文件工具類YmlUtils的詳細過程(不用引任何插件和依賴)

    Yml轉properties文件工具類YmlUtils的詳細過程(不用引任何插件和依賴)

    這篇文章主要介紹了Yml轉properties文件工具類YmlUtils(不用引任何插件和依賴),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • Ubuntu 15下安裝Eclipse經(jīng)驗分享

    Ubuntu 15下安裝Eclipse經(jīng)驗分享

    這篇文章主要為大家分享了Ubuntu 15下安裝Eclipse經(jīng)驗,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • SpringBoot+ThreadLocal+AbstractRoutingDataSource實現(xiàn)動態(tài)切換數(shù)據(jù)源

    SpringBoot+ThreadLocal+AbstractRoutingDataSource實現(xiàn)動態(tài)切換數(shù)據(jù)源

    最近在做業(yè)務需求時,需要從不同的數(shù)據(jù)庫中獲取數(shù)據(jù)然后寫入到當前數(shù)據(jù)庫中,因此涉及到切換數(shù)據(jù)源問題,所以本文采用ThreadLocal+AbstractRoutingDataSource來模擬實現(xiàn)dynamic-datasource-spring-boot-starter中線程數(shù)據(jù)源切換,需要的朋友可以參考下
    2023-08-08

最新評論

常宁市| 岐山县| 多伦县| 温州市| 新竹县| 襄垣县| 阳城县| 观塘区| 元朗区| 麦盖提县| 潼关县| 乌鲁木齐县| 安远县| 安溪县| 康乐县| 韶山市| 新昌县| 灵台县| 陇南市| 新河县| 陆丰市| 房山区| 腾冲县| 无为县| 昆明市| 容城县| 丹阳市| 栾川县| 夏河县| 栖霞市| 西和县| 吴江市| 天柱县| 七台河市| 剑川县| 武穴市| 芷江| 新化县| 中卫市| 深州市| 辉县市|