【问题标题】:apache shiro authorization annotations doesn't work with jsf 2.2apache shiro 授权注释不适用于 jsf 2.2
【发布时间】:2016-09-13 23:20:20
【问题描述】:

我将 apache shiro 1.2.4 与 jsf 2.2 和 glassfish 4.1 一起使用。

我在这里使用的是 xhtml 视图而不是 jsp。

我的身份验证工作正常,我可以在访问页面之前要求登录,授权通过使用 checkRole 以编程方式工作,但不能使用注释。

基本上我想要这个:

@RequiresRoles("Administrator")
public static void addEmployee(Employee emp,String plaintextpassword)
{                
    PasswordGenerator.generatePassword(emp, plaintextpassword);
    new TransactionExecuter<Employee,Void>().execute(new ObjectAdder<Employee>(), emp);        
}

只有管理员可以添加员工。

我在我的 netbeans 8.1 中添加了这些 jar:aspectj-jrt、aspectj-weaver、asm、cglib、shiro-aspectj 以及 shiro-core 和 shiro-web ofcourse(所有最新版本来自 maven repo)。

我在我的服务器日志中没有看到任何与此相关的错误,我已将 hibernate.show_sql 设置为 true,但我没有看到应该选择员工角色的语句。

下面是我的员工pojo:

@Entity
@Inheritance
public class Employee implements Serializable 
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)    
    private Integer id;

    @Basic(optional=false)
    @Column(unique=true)
    private String username;

    @Basic(optional=false)
    private String passwordhash;

    @Basic(optional=false)
    private String passwordsalt;

    @Basic(optional=false)
    private String firstname;

    @Basic(optional=false)
    private String lastname;

    private String address;

    @Basic(optional=false)
    private Integer salary;

    private String phonenumber;                   

    //getters and setters and constructor
}

你可以看到 @Inheritance 注释,我有 Administrator,DivisionManager,ManagementEmployee 扩展 Employee 没有额外的字段或方法。

鉴别器列是我的角色名称 :),正如我所说的 checkRole 有效 :)。

最后但并非最不重要的是我的 shiro.ini :

[main]
authc.loginUrl = /tmp/signin.xhtml
authc.successUrl = /tmp/employee_home.xhtml
logout.redirectUrl = /tmp/signin.xhtml

#Our Realm
jdbcRealm = com.model.realm.EmployeesRealm

# Sha256
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
# base64 encoding, not hex in this example:
sha256Matcher.storedCredentialsHexEncoded = false
sha256Matcher.hashIterations = 1024

jdbcRealm.credentialsMatcher = $sha256Matcher

# User Query
# default is "select password from users where username = ?"
jdbcRealm.authenticationQuery = SELECT passwordhash, passwordsalt FROM Employee WHERE username = ?

# permissions
jdbcRealm.permissionsLookupEnabled = true
jdbcRealm.userRolesQuery = select DTYPE from Employee where username = ?
jdbcRealm.permissionsQuery = select permission from EmployeePermission perm inner join Employee_EmployeePermission empperm on perm.id=empperm.permissions_id inner join Employee emp on emp.id=empperm.Employee_id where username = ?

#database
ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
ds.serverName = localhost
ds.user = root
ds.password = root
ds.databaseName = jsfdb
jdbcRealm.dataSource=$ds

authc.usernameParam = email
authc.passwordParam = password
authc.failureKeyAttribute = shiroLoginFailure

# Let's use some in-memory caching to reduce the number of runtime lookups against Stormpath.  A real
# application might want to use a more robust caching solution (e.g. ehcache or a distributed cache).  When using such
# caches, be aware of your cache TTL settings: too high a TTL and the cache won't reflect any potential
# changes in Stormpath fast enough.  Too low and the cache could evict too often, reducing performance.
cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $cacheManager

securityManager.realms = $jdbcRealm

[urls]
/* = authc
/tmp/signin.xhtml= anon

除了 shiro.ini 之外,我只有 web.xml 和 glassfish-resources.xml,我这么说是因为似乎有很多配置文件我不知道放在哪里。

注意1

我尝试集成 spring,但没有找到 SpringBeanFacesELResolver 所以我想切换到 AspectJ,我按照他们文档中的示例项目并添加了必要的依赖项(以及我在 stackoverflow 答案中找到的其他文件)。

我可以使用checkRole,但我希望它可以工作:)。

注2

我验证了上述 jars(aspectj-jrt 等)随战争一起部署,但仍然无法正常工作。

【问题讨论】:

    标签: jsf-2 authorization aop shiro


    【解决方案1】:

    对我来说它的工作。你在使用 maven 进行构建吗?对于我的情况,maven 是可能的,因为所有必需的依赖项都在那里,然后在编译时它称为 aspectj compile,它应用了注释并且注入工作正常。在此之前,我也遇到了让它工作的麻烦。试试这个https://github.com/apache/shiro/tree/master/samples/aspectj

    如果你能够让它工作,它也适用于 jsf

    【讨论】:

      猜你喜欢
      • 2012-09-30
      • 2011-12-06
      • 2014-08-31
      • 2015-12-01
      • 2012-01-24
      • 2017-06-10
      • 1970-01-01
      • 2016-06-06
      • 2016-03-07
      相关资源
      最近更新 更多