2016-12-08 20 views
0

我能夠使用Apache Shiro進行身份驗證,但現在我需要授權哪種方式從數據庫中獲取角色。查詢的工作,但只有當我使用整數角色。出於某種原因,它正在尋找角色是整數。這似乎沒有幫助,如果我不得不尋找數字作爲角色,我錯過了一些設置,告訴Shiro尋找一個字符串或varchar?Apache Shiro userRolesQuery只尋找一個int

這裏是我的Shiro.ini文件:

[main] 
ds = org.apache.shiro.jndi.JndiObjectFactory 
ds.requiredType = javax.sql.DataSource 
ds.resourceName = jdbc/SQL05 

jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm 
jdbcRealm.dataSource = $ds 
jdbcRealm.permissionsLookupEnabled = true 
jdbcRealm.authenticationQuery = SELECT company_password FROM CompanyUser where company_username = ? 
jdbcRealm.userRolesQuery = SELECT role FROM CompanyUser where company_username = ? 

Authentication.java

 System.setProperty("log4j.category.org.apache.shiro", "DEBUG");   
     Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); 
     SecurityManager securityManager = factory.getInstance(); 
     SecurityUtils.setSecurityManager(securityManager); 
     Subject currentUser = SecurityUtils.getSubject(); 
     String b = "false"; 
     log.info("WTF"); 
     try{ 
      if (!currentUser.isAuthenticated()) { 

       UsernamePasswordToken token = new UsernamePasswordToken("user1", "test") ; 
       token.setRememberMe(true); 
       try {       
        currentUser.login(token); 
        b = "You have successfully logged in user: " + token.getPrincipal().toString(); 
        if (currentUser.hasRole("1")) { 
         b += " and they have the role of being awesome"; 
        } else { 
         b += " and we can't find the role yet."; 
        } 

       }catch (UnknownAccountException uae) { 
        b = token.getPrincipal().toString(); 
        log.info("There is no user with username of " + token.getPrincipal().toString()); 
       } 
      } 
     }catch (AuthenticationException ae) { 
      b = ae.toString(); 
      log.info(b); 
     }catch(Exception e){ 
      b = e.toString(); 
      log.info(b); 
     } 

所以要清楚,這個當前工作,因爲我有:

currentUser.hasRole("1") 

在那裏和角色字段有一個1用於該用戶名。

Severe: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the nvarchar value 'Awesome' to data type int.

那麼,如何得到它接受的字符串:如果我在兩個地方我得到以下錯誤改變1爲「真棒」(不帶引號)?

我的表CompanyUser:

userid int not null auto increment

personid int not null

company_username varchar(15) not null

company_password varchar(15) not

null role varchar(20) not null

出於某種原因,它試圖列「角色」轉換爲int。

+0

你確定你的角色字段的數據庫列有正確的數據類型嗎?也許它被聲明爲int,所以它試圖將任何輸入解釋爲整數 – Reek

+1

對於我來說,帶有字符串作品的角色。請張貼您的表格結構。 – dev

+0

更新了所需信息的問題。 –

回答

0

您應該使用currentUser.hasRole("awesome")currentUser.hasRole(awesome)

是awesomea變量?如果是這樣,請舉例說明真棒。

+0

上面我給出的例子是工作示例,其中數據庫有1個變量(數據庫列是否是varchar的int似乎並不重要,如果是字符串,它將在字符串只有數字時起作用)所以java:if(currentUser.hasRole(「1」)。將角色作爲字符串是什麼給了我這個錯誤,它正在尋找一個奇怪的原因數字 –

+0

對我來說它也適用於字符串。有一個角色「GOD」,它被映射到權限「*」,它的工作原理 – dev

+0

你可以嘗試角色查詢作爲SELECT CAST(角色AS NVARCHAR(50))從公司用戶其中company_username =?你也使用權限,因爲我看到jdbcRealm.permissionsLookupEnabled =真如果沒有任何權限查詢。如果你不使用它使它假。看起來它通過驅動程序的cponversion的SQL Server問題它適用於我的oracle。 – dev