如何使用Spring Boot连接资源文件夹中的html文件?

泉泉 占柱 :

我的SpringWebConfiguration.class在这里:

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    private static Logger logger = LoggerFactory.getLogger(SpringSecurityConfig.class);

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        logger.info("-----configure(HttpSecurity http)");

        http.authorizeRequests()
                .antMatchers("/**").permitAll()
                .antMatchers("/admin/**").hasAnyRole("ADMIN")
                .antMatchers("/user/**").hasAnyRole("USER")
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginProcessingUrl("/login")
                .loginPage("/login")//
                .defaultSuccessUrl("/userAccountInfo")//
                .failureUrl("/login?error=true")//
                .usernameParameter("username")//
                .passwordParameter("password")
                .defaultSuccessUrl("/")
                .permitAll().
                and().rememberMe().rememberMeParameter("remember-me").key("uniqueAndSecret").tokenValiditySeconds(1296000).userDetailsService(userDetailsService)
                .and()
                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/")
                .deleteCookies("guid")
                .deleteCookies("JSESSIONID")
                .permitAll()
                .and().csrf().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        logger.info("-----configureGlobal(AuthenticationManagerBuilder auth)");
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }
}

我的LoginController:

@RestController
public class LoginController() {
@GetMapping("/login")
    public String login(Model model) {
        return "/login";
    }
}

我的html文件:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <h2>Stacked form</h2>
    <form th:action="@{/login}" method="post">
        <div class="form-group">
            <label for="email">Email:</label>
            <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
        </div>
        <div class="form-group">
            <label for="pwd">Password:</label>
            <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pswd">
        </div>
        <div class="form-group form-check">
            <label class="form-check-label">
                <input class="form-check-input" type="checkbox" name="remember"> Remember me
            </label>
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</div>
</body>
</html>

当我打开浏览器并转到“ localhost:8080 / login”时。它返回字符串“ / login”,而不是html登录页面。为什么?也许我错过了一些东西来连接到我的HTML文件。我认为我的控制器需要类似url的内容才能连接到html文件。我不知道它如何正常工作。请帮帮我!

Atul Jain:

首先,您需要修改LoginController

@Controller
public class LoginController() {
  @GetMapping("/login")
  public String login(Model model) {
        return "/login";
  }
}

如果不起作用,则将html转换为jsp页面。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Spring Boot中访问src / main / resources /文件夹中的资源文件

如何最好地从资源文件夹访问文件

如何使用gradle复制classes文件夹中的资源文件?

如何将FileInputStream获取到资源文件夹中的文件

将外部资源文件夹添加到Spring Boot

部署后将文件存储在spring boot资源文件夹中

如何从资源文件夹加载文件?

如何在资源文件夹中引用javafx fxml文件?

将路径传递到资源文件夹中的文件以定义Spring Bean

使用@sql spring boot test从自定义位置而不是资源文件夹中读取Reading.sql文件

Thymeleaf和Spring Boot,如何构造资源文件夹以在运行时和设计时查看html

如何从资源文件夹中正确获取文件

如何从SpringBoot应用程序的资源文件夹中读取所有文件?

在Spring中将文件写入资源文件夹

Spring Boot无法从资源文件夹中提供静态图像

如何在Spring MVC中获取资源文件夹的正确路径?

如何在gradle和其他资源文件夹中添加资源文件夹

使用IntelliJ启动时,Spring Boot不提供资源文件夹中的资产

如何从资源文件夹中获取文件作为文件?

Spring Boot和Thymeleaf资源文件夹问题

如何从资源文件夹中删除Android徽标

如何将XML连接到资源文件夹

如何访问IDE(eclipse)资源文件夹中的数据文件?

Spring Boot:找不到位于带有@Value 注释的资源文件夹中的文件

我如何从 java spring-boot AZURE 函数中的资源文件夹中读取任何文件?

WinForms如何打开资源文件夹中的文件

如何使用 IDE 和 Java -jar 命令读取 Spring Boot 应用程序中资源文件夹中存在的文件?

Spring Boot 在资源文件夹中找不到 beans.xml

在资源文件夹中上传图片 Spring boot