Apache Shiro:登录时出现IllegalArgumentException

Ali*_*abi 5 java security jsf shiro

使用Apache Shiro时,登录时会出现以下异常:

java.lang.IllegalArgumentException:配置错误.配置错误.具有属性[loginUrl]的指定对象[authc],而不首先定义该对象的类.请先指定类属性,例如myObject = fully_qualified_class_name,然后定义其他属性.

shiro.ini

  # ----------------------------------------------------------------------------- 
  [main]
  authc.loginUrl=/login.xhtml
  authc.successUrl=/hello.xhtml
  logout.redirectUrl=/hello.xhtml

  # Users and their (optional) assigned roles
  # username = password, role1, role2, ..., roleN
  # -----------------------------------------------------------------------------
  [users]
  root = secret, admin
  guest = guest, guest

  # -----------------------------------------------------------------------------
  # Roles with assigned permissions
  # roleName = perm1, perm2, ..., permN
   -----------------------------------------------------------------------------
 [roles]
 admin = *
 schwartz = lightsaber:*
 goodguy = winnebago:drive:eagle5

  #------------------------------------------------------------------------------
 [urls]
 /hello.xhtml= authc 
Run Code Online (Sandbox Code Playgroud)

调节器

public void login() {
    Factory<SecurityManager> factory = new IniSecurityManagerFactory();
    SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);
    Subject currentUser=SecurityUtils.getSubject();

    if(!currentUser.isAuthenticated()){
        UsernamePasswordToken token=new UsernamePasswordToken("root","secret");
        token.setRememberMe(true);
        try{
            currentUser.login(token);
        }catch(UnknownAccountException e){
            System.out.println("username is incorrect");
        }catch (IncorrectCredentialsException e) {
            System.out.println("password is incorrect");
        }catch (LockedAccountException e) {
            System.out.println("account was locked");
        }catch (AuthenticationException e) {
            System.out.println("there are some error");
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

web.xml中

<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

Pau*_*l H 7

尝试使用PassThruAuthenticationFilter从控制器执行登录尝试.将此行添加到shiro.ini:

authc = org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter
Run Code Online (Sandbox Code Playgroud)

接下来,当您从web.xml文件启动Shiro Security Manager时,可以从该login()方法中删除以下代码行:

Factory<SecurityManager> factory = new IniSecurityManagerFactory();
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Run Code Online (Sandbox Code Playgroud)

请注意,这FormAuthenticationFilter是另一种类型的身份验证筛选器,它还有助于处理登录请