首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在spring引导中使用Springsecurity.xml中的配置?

如何在spring引导中使用Springsecurity.xml中的配置?
EN

Stack Overflow用户
提问于 2018-11-20 12:39:22
回答 1查看 4.1K关注 0票数 1

我正在尝试在spring引导中使用我现有的spring安全xml文件。我添加了@ImportResource来加载我现有的xml配置。在控制台上,它显示加载Springsecurity.xml。

我正在犯以下错误:

代码语言:javascript
复制
 "Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration$DefaultConfigurerAdapter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor<?>' available"

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor' in your 

OnlineshoppingApplication.java

代码语言:javascript
复制
package net.kzn.onlineshopping;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ComponentScan(basePackages = {"net.kzn.onlineshopping","net.kzn.shoppingbackend"})
@ImportResource({"classpath:spring-security.xml","classpath:dispatcher-servlet.xml"})
public class OnlineshoppingApplication {

    public static void main(String[] args) {
        SpringApplication.run(OnlineshoppingApplication.class, args);
    }


}

spring-security.xml

代码语言:javascript
复制
<beans:beans
    xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/security
        http://www.springframework.org/schema/security/spring-security.xsd">

    <context:component-scan
        base-package="net.kzn.shoppingbackend" />
    <beans:bean id="passwordEncoder" 
            class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>      
    <http pattern="/resources/**" security="none"/>

    <http>
        <!-- only admin access -->
        <intercept-url pattern="/manage/**" access="hasAuthority('ADMIN')" />
        <!-- only user access (who is registered) -->
        <intercept-url pattern="/cart/**" access="hasAuthority('USER')" />
        <!-- rest of the world -->  
        <intercept-url pattern="/**" access="permitAll" />
        <form-login login-page="/login"/>
        <access-denied-handler error-page="/access-denied"/>
    </http>

    <authentication-manager>
        <authentication-provider>
        <password-encoder ref="passwordEncoder"/>
            <jdbc-user-service data-source-ref="dataSource"
                users-by-username-query="select email, password, enabled from user_detail where email = ?"
                authorities-by-username-query="select email, role from user_detail where email = ?" />
        </authentication-provider>
    </authentication-manager>

</beans:beans>

我的pom.xml文件

pom.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.kzn</groupId>
    <artifactId>shoppingbackend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>shoppingbackend</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-browser</artifactId>
        </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.webflow/spring-webflow -->
        <dependency>
            <groupId>org.springframework.webflow</groupId>
            <artifactId>spring-webflow</artifactId>
            <version>2.5.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-20 13:03:01

我建议您共享pom.xml文件,并尝试在springboot应用程序中使用下面的注释,因为缺少的东西不多。

根据您的设置,如果您收到任何与引导自动配置相关的启动错误,您可能必须排除一些自动配置类,例如将其放入应用程序类:

代码语言:javascript
复制
@EnableAutoConfiguration(exclude = [SecurityFilterAutoConfiguration, SpringBootWebSecurityConfiguration])

还添加了带有spring的@EnableWebSecurity注释。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53393196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档