gpt4 book ai didi

java - 启用spring security后静态资源不再可用

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:06 25 4
gpt4 key购买 nike

我与一些配置作斗争,导致 Spring 应用程序中的静态资源(js、css 等)不再可用。有人可以指出我错过了什么吗?这是代码。请注意,我的 JS 和 CSS 位于以下文件夹中。另请注意,在启用 Spring Security 之前,这些资源是可以访问的

Web Pages ->
WEB-INF ->
resources ->
and here 2 folders js and css

在我的 JSP 页面中,我想通过以下链接访问 js 文件夹中的 jquery

<script src="resources/js/jquery.js"></script>

浏览器中的错误是

加载资源失败:http://localhost:8010/resources/js/js.js服务器响应状态为 404(未找到)

@Configuration
@EnableScheduling // if you want certain function running on the backend on recurrning basis like very 5 second a function call itself then you have to ensbale this. this will enable all classes defined as Service and Scheduled to be running on backend automatically. see example in com.outbottle.controllers.ScheduledTasks.java
@ComponentScan("com.outbottle, com.JavaAnatatedClass") //this will import all beans marked as @Controller, or @Repository, or @Service, or @Component in the application context
@ImportResource({"classpath:beans1.xml"}) //this will import all beans in the application context from this xml file
@EnableWebMvc
public class Config extends WebMvcConfigurerAdapter {

@Bean
public UrlBasedViewResolver setupViewResolver() {
.....
.....
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/**");

}

这是安全配置文件。

@Configuration
@EnableWebSecurity
//@PropertySource("classpath:jdbc.properties")
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {

AuthenticationService as = new AuthenticationService();
auth.userDetailsService(as);
}


@Override
protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()
.antMatchers("/", "/home", "/REST").permitAll()
.antMatchers("/resources/**").permitAll()
.antMatchers("/admin/**").access("hasAuthority('ADMIN')") //you have to use hasAuthority because in AuthenticationService you are using GrantedAuthority object. i replaced hasRole with hasAuthority. check the detailes below in comments
.antMatchers("/db/**").access("hasAuthority('ADMIN') and hasAuthority('DBA')")

//Custom Login Form with fields and values
//.and().formLogin() //this will show default spring login page
.and().formLogin().loginPage("/login")
.usernameParameter("ssoId").passwordParameter("password") //these aret the fields on the login page for user and password
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/Access_Denied");

}

最佳答案

我做了一些尝试,发现这个有效

registry.addResourceHandler("/resources/js/*").addResourceLocations("/WEB-INF/resources/js/");
registry.addResourceHandler("/resources/css/*").addResourceLocations("/WEB-INF/resources/css/");

由于某些原因/WEB-INF/resources/js/* 或 ** 不工作

我还将每个目录单独添加到资源中。

沙赫扎德

关于java - 启用spring security后静态资源不再可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38375118/

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