gpt4 book ai didi

java - Spring Securing Web 添加 css、js 和图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:39 24 4
gpt4 key购买 nike

我想将 css、javascript 和图像添加到 spring 4 web 应用程序,我可以举个例子,但我正在尝试 spring 教程:

保护网络 - http://spring.io/guides/gs/securing-web/

我添加了以下更改:

package hello;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}

然后我在资源文件夹中添加了一个带有简单 css 的文件夹 css:

@CHARSET "ISO-8859-1";

p {
border-style:solid;
border-width:5px;
}

然后我将 css 添加到 home.html:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>
<link type="text/css" rel="stylesheet" href="/resources/css/dummy.css"></link>
</head>
<body>
<h1>Welcome!</h1>

<p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
</body>
</html>

安全配置通过以下 java 类注入(inject):

    package hello;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}

然后当使用谷歌调试时它返回代码

Status Code:302 Found but the css is not applied.

任何人都可以指出要添加以使其正常工作所需的更改吗?

最佳答案

经过一番搜索发现了问题,示例中需要能够使用css的更改是:在 src/main 下创建以下目录:

- webapp
- resources
- css

然后添加 dummy.css 并在所需的 html 中使用:

<link th:href="@{/resources/css/dummy.css}" type="text/css" rel="stylesheet" href="/resources/css/dummy.css"></link>

感谢所有试图提供帮助的人:)

关于java - Spring Securing Web 添加 css、js 和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22050277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com