Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Shiro配置_Spring Mvc_Spring Data Jpa_Shiro - Fatal编程技术网

Spring mvc Shiro配置

Spring mvc Shiro配置,spring-mvc,spring-data-jpa,shiro,Spring Mvc,Spring Data Jpa,Shiro,我正在尝试一个关于如何使用shiro为管理员创建登录名的教程。我在做shiro配置的时候累了。我只有两个页面:一个管理员页面和一个管理员的主登录页面 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/j2ee/

我正在尝试一个关于如何使用shiro为管理员创建登录名的教程。我在做shiro配置的时候累了。我只有两个页面:一个管理员页面和一个管理员的主登录页面

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             version="2.4">
        <display-name>LoginTutorial</display-name>
        <filter>
            <filter-name>shiroFilter</filter-name>
            <filter-class>org.apache.shiro.web.servlet.iniShiroFilter</filter-class>
            <init-param>
                <param-name>config</param-name>
                <param-value>
                    [main]
                    realm = 
                    securityManager.realm = $realm
                    authc.loginUrl = /loginpage.jsp 

                    [user]
                    Admin = password,ROLE_ADMIN

                    [roles]
                    ROLE_ADMIN = *

                    [url]
                    <!--/account/** =authc-->
                    /adminpage = roles[ROLE_ADMIN]
                </param-value>
            </init-param>
        </filter>

        <filter-mapping>
            <filter-name>ShiroFilter</filter-name>
            <url-pattern>/</url-pattern>
        </filter-mapping>
        ...
</web-app>

后勤室
雪洛菲特
org.apache.shiro.web.servlet.iniShiroFilter
配置
[主要]
领域=
securityManager.realm=$realm
authc.loginUrl=/loginpage.jsp
[用户]
管理员=密码,角色\管理员
[角色]
角色\管理=*
[网址]
/adminpage=角色[角色\管理员]
雪洛菲特
/
...

您正在使用Spring框架吗? 通常,您应该在Web.xml中定义Shiro过滤器,并在applicationContext.xml中初始化Shiro组件(作为bean)

例如,您可以执行以下操作:

Web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             version="2.4">
   <display-name>LoginTutorial</display-name>
<!-- Shiro filter-->
    <filter>
        <filter-name>ShiroFilter</filter-name>

        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>ShiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
...
<?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:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:couchdb="http://www.ektorp.org/schema/couchdb"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.ektorp.org/schema/couchdb
                        http://www.ektorp.org/schema/couchdb/couchdb.xsd
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

  <!-- Scans within the base package of the application for @Components to configure as beans -->
  <!-- Apache Shiro customized classes are defined in the package com.6.0.shiro -->

    <context:component-scan base-package="com.6.0.shiro" />
...

  <!-- Shiro filter -->
    <bean id="ShiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="filters">
            <util:map>
                <entry key="myAuthcBasic">
                    <bean class="org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter"/>
                </entry>
            </util:map>
        </property>
        <property name="filterChainDefinitions">
            <value> 
                  /safe/** = myAuthcBasic
            </value>
        </property>
    </bean>
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
        <property name="realm" ref="StaticRealm"/>
        <property name="cacheManager" ref="cacheManager"/>
        <!-- By default the servlet container sessions will be used.  Uncomment this line
        to use shiro's native sessions (see the JavaDoc for more): -->
        <!-- <property name="sessionMode" value="native"/> -->
    </bean>
    <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager">
    <!--property name="cacheManager" ref="ehCacheManager" /-->
    </bean>
    <!-- Define the Shiro Realm implementation you want to use to connect to your back-end -->
    <!-- StaticRealm: -->
    <bean id="StaticRealm" class="com.6.0.shiro.StaticRealm">
        <property name="credentialsMatcher" ref="credMatcher">
        </property>
    </bean>
    <bean id="credMatcher" class="com.example.shiro.ReverseCredentialsMatcher"/>
    ...

后勤室
雪洛菲特
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
真的
雪洛菲特
/*
...
applicationContext.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             version="2.4">
   <display-name>LoginTutorial</display-name>
<!-- Shiro filter-->
    <filter>
        <filter-name>ShiroFilter</filter-name>

        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>ShiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
...
<?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:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:couchdb="http://www.ektorp.org/schema/couchdb"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.ektorp.org/schema/couchdb
                        http://www.ektorp.org/schema/couchdb/couchdb.xsd
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

  <!-- Scans within the base package of the application for @Components to configure as beans -->
  <!-- Apache Shiro customized classes are defined in the package com.6.0.shiro -->

    <context:component-scan base-package="com.6.0.shiro" />
...

  <!-- Shiro filter -->
    <bean id="ShiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="filters">
            <util:map>
                <entry key="myAuthcBasic">
                    <bean class="org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter"/>
                </entry>
            </util:map>
        </property>
        <property name="filterChainDefinitions">
            <value> 
                  /safe/** = myAuthcBasic
            </value>
        </property>
    </bean>
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
        <property name="realm" ref="StaticRealm"/>
        <property name="cacheManager" ref="cacheManager"/>
        <!-- By default the servlet container sessions will be used.  Uncomment this line
        to use shiro's native sessions (see the JavaDoc for more): -->
        <!-- <property name="sessionMode" value="native"/> -->
    </bean>
    <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager">
    <!--property name="cacheManager" ref="ehCacheManager" /-->
    </bean>
    <!-- Define the Shiro Realm implementation you want to use to connect to your back-end -->
    <!-- StaticRealm: -->
    <bean id="StaticRealm" class="com.6.0.shiro.StaticRealm">
        <property name="credentialsMatcher" ref="credMatcher">
        </property>
    </bean>
    <bean id="credMatcher" class="com.example.shiro.ReverseCredentialsMatcher"/>
    ...

...
/safe/**=myAuthcBasic
...

你的问题是什么?抱歉..只是不知道我是否走在正确的道路上…就好像这是正确的一样Hi@bourigue..谢谢..但我已经让它工作了..我做了与你发布的相同的事情。好的Samuel!我希望它能帮助别人。祝你今天愉快@SamuelAnertey请接受此答案以结案,因为这是您问题的解决方案。