当前位置: 首页 > 知识库问答 >
问题:

authc筛选器没有用spring调用shiro中的MyRealm

梁豪
2023-03-14

我正在用Spring/Hibernate/JAX-RS(jersey)做项目。我想把四郎整合到我的项目中。我按照文档中的方式进行配置。我有自己的Realm实现。

问:问题是当调用authBasic路径时,它与MyRealm很好地配合。但是当我调用authc(FormAuthenticationFilter)时,它会显示登录页面,而当我输入凭据时,它会再次进入登录页面,而不会调用MyRealm。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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>Restful Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

     <!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml -->
    <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>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>kg.enesaitech.barbulak.ws</param-value>            
        </init-param>       
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>
<?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:tx="http://www.springframework.org/schema/tx"

    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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="kg.enesaitech.barbulak.*" />



    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login.html"/>
        <property name="successUrl" value="/apps/warehause_manager"/>
        <property name="filterChainDefinitions">
            <value>
                /logout = logout
                /apps/admin/** = authcBasic
                /apps/accountant/** = authc
                /apps/production_manager/** = authc, roles[admin]
            </value>
        </property>
    </bean>

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm"/>
    </bean>
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <bean id="myRealm" class="kg.enesaitech.barbulak.security.MyRealm"></bean>  




    <bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
             <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="barbulak" />
    </bean>
    <bean id="mapper" class="org.dozer.DozerBeanMapper" lazy-init="false">
        <property name="customFieldMapper" ref="dozerCustomFieldMapper" />

    <bean id="dozerCustomFieldMapper" class="kg.enesaitech.barbulak.providers.MyCustomFieldMapper" />

    <tx:annotation-driven/>




</beans>
package kg.enesaitech.barbulak.security;



public class MyRealm extends AuthorizingRealm {

    UsersHome usersHome = new UsersHome();
    RoleHome roleHome = new RoleHome();

    protected boolean permissionsLookupEnabled = false;

    public MyRealm() {
        super();
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
                throws org.apache.shiro.authc.AuthenticationException {

        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername();

        AuthenticationInfo info = null;
        Users user = usersHome.getByUserName(username);

        if(user == null || user.getUserPass() == null){
            throw new UnknownAccountException("No account found for user [" + username + "]");
        }
        info = new SimpleAuthenticationInfo(username, user.getUserPass().toCharArray(), getName());

        return info;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        if (principals == null) {
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
        }

        String username = (String) getAvailablePrincipal(principals);
        System.out.println("Auth | username : " + username);


        Set<String> roleNames = roleHome.getNameSetByUserName(username);


        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
        return info;
    }

}

共有1个答案

段弘和
2023-03-14

我的问题是将/login.html=authc添加到url中,对于像我这样搜索这一行3天的其他人来说。

 类似资料:
  • 我在lexer语法文件中编写并用antlr4编译它。上面说 看来“filter”只适用于ANTLR3 我查了wikihttp://www.antlr.org/wiki/display/antlr4/options,没有找到答案。“filter”关键字也不在源文件https://github.com/antlr/antlr4/blob/master/tool/src/org/antlr/v4/too

  • 我正在尝试将基于Spring MVC xml的项目配置重构为基于Spring Boot java的配置。同时按如下方式设置shiro配置:

  • 下面是过滤器的外观。 对于WebSecurityConfigurerAdapter#configure(webSecurityWeb)web.忽略()中包含的路径,我希望该筛选器不会像Spring Security筛选器链的其余部分一样激发。 下面是它的样子 我希望在调用Spring Security链的其余部分时调用这个过滤器,并对web.igneration()中的路径忽略这个过滤器,就像Sp

  • 我对这个例外很困惑。有人能帮忙吗? 我使用的是Spring Boot 2.1.6。release和shiro-spring-boot-starter 1.4.0。

  • 应用筛选器之前的示例使用者记录是(在值中查找GP_ID): 当我在kafkaListenerContainerFactory()中按如下方式设置recordFilterStrategy时: KafKareCordvo.ConvertByteBufferToLong正在将bytebuffer值转换为long值。 但是,当它被Kafka听众按以下方式消费时: 这将返回删除我筛选的字段值的记录:“gp_

  • “{”error“:{”root_case“:[{”type“:”parsing_exception“,”reason“:”[match_all]格式错误的查询,预期[END_OBJECT]但找到[FIELD_NAME]“,”line“:”line“:1,”col“:26}”,“status”:400}“,”type“:”parsing_exception“,”reason“:”[match_al