首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Security添加到我的Spring应用程序中,自动将所有路径重定向到一个完全不同的项目中设置的路径

将Security添加到我的Spring应用程序中,自动将所有路径重定向到一个完全不同的项目中设置的路径
EN

Stack Overflow用户
提问于 2019-01-02 21:24:47
回答 1查看 187关注 0票数 0

我添加了依赖项(我使用Maven)来保护我的应用程序(在教程之后);问题是,一旦我添加它,每当我到本地主机检查进度时,我总是被重定向到另一个项目中编码的路径(我现在甚至找不到视图)。

我目前遵循关于TutorialsPoint的教程,但是我使用弹簧工具套房4来添加依赖项、编码和运行我的Spring应用程序,而不是在localhost上运行CLI :8080。

我试着找出有问题的视图,修改另一个项目的web.xml,但我找不到问题的根源,我只知道不知怎么搞砸了。

Thymeleaf模板

代码语言:javascript
复制
<!DOCTYPE html>
<html>
   <head>
      <meta charset = "ISO-8859-1" />
      <link href = "css/styles.css" rel = "stylesheet"/>
      <title>Spring Boot Application</title>
   </head>
   <body>
      <h4>Welcome to Thymeleaf Spring Boot web application</h4>
   </body>
</html>

Web控制器

代码语言:javascript
复制
package com.example.vj7.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WebController {
   @RequestMapping(value = "/index")
   public String index() {
      return "index";
   }
}

应用类

代码语言:javascript
复制
package com.example.vj7;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>vj7</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Vj7</name>
    <description>Vježba 7</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

"localhost:8080“的预期结果只是一个标题"Demo”(就像在index.html中,在web控制器中设置),但是我以前在另一个Spring项目中使用的表单出现在路径"/login“中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-03 01:20:29

如果您希望不经授权地访问index.html,则需要覆盖一些web安全的默认配置。取自https://docs.spring.io/spring-security/site/docs/4.2.5.RELEASE/apidocs/org/springframework/security/config/annotation/web/configuration/EnableWebSecurity.html

代码语言:javascript
复制
 @Configuration
 @EnableWebSecurity
 public class MyWebSecurityConfiguration extends WebSecurityConfigurerAdapter {


        @Override
        protected void configure(HttpSecurity http) throws Exception {
                http.authorizeRequests().antMatchers("/", "/index").permitAll().anyRequest()
                                .permitAll();
        }
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54013373

复制
相关文章

相似问题

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