首页 > 解决方案 > 如何在现有的基于xml的spring mvc项目中使用注释配置spring security?

问题描述

我是 Spring Security 的新手,并尝试使用注释在现有 Spring 项目中设置 Spring Security(授权和身份验证),但没有获得默认的 Spring 登录页面。

请指教,我做错了什么。

我尝试使用 xml 方法,它工作正常,但 xml 和注释方法的组合不起作用。

示例 web.xml 定义为:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

spring-mvc-servlet.xml 定义为

<!-- Step 1: Add support for component scanning -->
<context:component-scan base-package="com.aditya.delete" />

<!-- Step 2: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 3: Define Spring MVC view resolver -->
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

我的配置类定义为:

    @Configuration
    @EnableWebSecurity
    public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    UserBuilder users = User.withDefaultPasswordEncoder();

    auth.inMemoryAuthentication().withUser(users.username("aditya").password("abc").roles("EMPLOYEE"));

}

扩展 AbstractSecurityWebApplicationInitializer 的类定义为:

    public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {}

控制器定义为:

    @Controller
    public class MyWebController {


@RequestMapping
public String showHome() {

    return "home";
}

}

当我运行 Web 应用程序时,它会直接重定向到我的 home.jsp 页面,而不是重定向到默认的 spring 登录页面。

pom.xml 包含

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myproject.aditya</groupId>
    <artifactId>deleteProject</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>deleteProject Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Servlet Dependency -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- JSP Dependency -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>


        <!-- JSTL dependency -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>



        <!-- Spring mvc dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>


        <!-- Intermittent dependency for spring-tx (used for spring-hibernate integration) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

        <!-- Intermittent dependency for spring-orm (used for spring-hibernate 
            integration) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.3</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>deleteProject</finalName>
    </build>
</project>

项目结构截图

标签: javaspring-security

解决方案


推荐阅读