简体   繁体   English

如何在Spring Boot中使用spring-security.xml中的配置?

[英]How to use configuration from spring-security.xml in spring boot?

I am trying to use my existing spring security xml file in spring boot. 我正在尝试在Spring Boot中使用现有的Spring Security xml文件。 I added @ImportResource to load my existing xml configuration. 我添加了@ImportResource来加载现有的xml配置。 on console its showing loading spring-security.xml. 在控制台上,其显示正在加载spring-security.xml。

I am getting below error: 我得到以下错误:

 "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 OnlineshoppingApplication.java

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 弹簧security.xml文件

<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>

my pom.xml file 我的pom.xml文件

pom.xml 的pom.xml

<?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>

How can i solve this problem? 我怎么解决这个问题?

I would suggest your to please share pom.xml file and also try to use below annotation with springboot application as few things are missing. 我建议您请共享pom.xml文件,并尝试将以下注释与springboot应用程序一起使用,因为缺少了一些东西。

depending on your setup, if you get any startup error related to boot autoconfiguration, you might have to exclude some auto-configuration classes, eg by putting this onto your Application class: 根据您的设置,如果遇到与启动自动配置有关的任何启动错误,则可能必须排除一些自动配置类,例如,将其放入Application类中:

@EnableAutoConfiguration(exclude = [SecurityFilterAutoConfiguration, SpringBootWebSecurityConfiguration])

also add @EnableWebSecurity annnotation with spring-boot. 还要在spring-boot中添加@EnableWebSecurity注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于Spring-security.xml代码的配置 - Spring-security.xml code based configuration 我正在关注基于Spring xml的配置,因此如何在spring-security.xml文件中配置TokenBasedRememberMeServices? - I am following spring xml based configuraiton so How TokenBasedRememberMeServices configuration in spring-security.xml file? spring-security.xml中的MySQL查询以进行授权 - MySQL query in spring-security.xml for authorization spring-security.xml中的密码更改 - password change in spring-security.xml 为什么我不能将用@Service注释的bean注入到spring-security.xml配置文件中声明的bean中? - Why I can't inject this bean annoted with @Service into a bean declared into the spring-security.xml configuration file? Spring Boot中Spring Security的XML配置 - XML configuration of Spring Security in Spring Boot 没有文件的嵌入式样式表指令:spring-security.xml - No embedded stylesheet instruction for file: spring-security.xml 如何在没有配置的情况下将spring boot security用于自定义登录页面? - How to use spring boot security for custom login page without configuration? WebSecurityConfig Java等效于spring-security.xml的Spring LDAP认证 - WebSecurityConfig Java equivalent for spring LDAP authentication done with spring-security.xml Spring Security从XML配置到Java配置 - Spring security from xml configuration to java config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM