Spring Security 静态资源访问
在搞 Spring Security 的时候遇到了一个小坑,就是静态资源加载的问题。
当我们继承了 WebSecurityConfigurerAdapter的时候,会去重写几个方法。去设定我们自己要过滤的路径或者是权限的一些规则。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
CustomUserService customUserService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserService).passwordEncoder(new BCryptPasswordEncoder());
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/global/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 开始请求权限配置
.authorizeRequests()
// 我们指定任何用户都可以访问多个URL的模式。
// 任何用户都可以访问以"/resources/","/signup", 或者 "/about"开头的URL。
// .antMatchers("/global/**","/static/**").permitAll()
// 请求匹配 /admin/** 只拥有 ROLE_ADMIN 角色的用户可以访问
.antMatchers("/admin/**").hasRole("ADMIN")
// 请求匹配 /user/** 拥有 ROLE_ADMIN 和 ROLE_USER 的角色用户都可以访问
.antMatchers("/user/**").hasAnyRole("ADMIN", "USER")
// 任何以"/db/" 开头的URL需要同时具有 "ROLE_ADMIN" 和 "ROLE_DBA"权限的用户才可以访问。
// 和上面一样我们的 hasRole 方法也没有使用 "ROLE_" 前缀。
// .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
// 其余所有的请求都需要认证后才可以访问
.anyRequest().authenticated().and().formLogin()
// 登陆界面;默认登陆成功后的界面(不起作用);默认登陆失败的界面;表单提交地址
.loginPage("/login").defaultSuccessUrl("/index.html").failureUrl("/login?error=true")
// 默认用户名键值,默认密码键值
.usernameParameter("username").passwordParameter("password").permitAll().and().rememberMe()
.tokenValiditySeconds(1209600).key("rememberme");
// .and()
// .logout().logoutUrl("").logoutSuccessUrl("/index.html").permitAll();
}
}
在一般来看来,我设置了
// 任何用户都可以访问以"/resources/","/signup", 或者 "/about"开头的URL。
.antMatchers("/global/**","/static/**").permitAll()
或者是
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/global/**");
}
之后应该没有什么问题,就应该可以访问到了我们的资源。可是当你运行起demo之后,你会发现,世界并不是你想象的那个样子。你还太年轻。
你所要的静态资源还是加载不出来。后来发现,我们还需要去配置一下 SpringMVC 里的 addResourceHandlers 方法。
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
// TODO Auto-generated method stub
// 注册访问 /login 转向 page-login.html 页面
registry.addViewController("/login").setViewName("page-login.html");
super.addViewControllers(registry);
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// TODO Auto-generated method stub
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
}
看起来,这次应该就可以了吧。 Run ...
可是还是太年轻。依旧没有加载到资源。
这个,这个就有点凌乱了。。。。
过了好久好久好久,睡了一觉起来。
原来是HTML出了问题。对,没有听错是 HTML 出了问题。
在加载 css 或者是 js 资源的时候,我们要写的更加标准一些。
<link href="/global/css/style.css" rel="stylesheet" type="text/css" />
<script src="/global/js/custom.min.js" type="text/javascript"></script>
而不是
<link href="/global/css/style.css"/>
<script src="/global/js/custom.min.js"></script>
Spring Security 静态资源访问的更多相关文章
- Spring Security静态资源访问
在使用Spring Security时要求所有请求都需要授权访问,此时会定义过滤规则如下 protected void configure(HttpSecurity http) throws Exce ...
- Spring Boot 静态资源访问原理解析
一.前言 springboot配置静态资源方式是多种多样,接下来我会介绍其中几种方式,并解析一下其中的原理. 二.使用properties属性进行配置 应该说 spring.mvc.static-pa ...
- 7.Spring MVC静态资源访问
在SpringMVC中常用的就是Controller与View.但是我们常常会需要访问静态资源,如html,js,css,image等. 默认的访问的URL都会被DispatcherServlet所拦 ...
- springboot配置spring security 静态资源不能访问
在springboot整合spring security 过程中曾遇到下面问题:(spring boot 2.0以上版本 spring security 5.x (spring secur ...
- Spring MVC静态资源访问
最近在学习servlet的时候发现自己不能访问到css和js, 于是google一番学到不少方法加载,总结如下: 1.对于Spring MVC, 由于我们截获了所有请求<url-pattern& ...
- spring mvc静态资源访问的配置
如果我们使用spring mvc来做web访问请求的控制转发,那么默认所有访问都将被DispatcherServlet独裁统治.比如我现在想访问的欢迎页index.html根本无需任何业务逻辑处理,仅 ...
- spring mvc官网下最新jar搭建框架-静态资源访问处理-注解-自动扫描
1.从官网下载spring相关jar http://spring.io/projects 点击SPRING FRAMEWORK
- 【spring】静态资源的访问受限解决方法
前言 我们知道在整合spring mvc框架的时候需要在web.xml中配置一个servlet 代码如下 <!--spring mvc 的DispatcherServlet--> < ...
- Spring MVC 使用介绍(十一)—— 跨域与静态资源访问
一.跨域 服务端须在响应中添加相应响应头,从而允许跨域,具体可通过 public class CorsFilter extends OncePerRequestFilter { @Override p ...
随机推荐
- 详解shuffle过程(转载)
http://langyu.iteye.com/blog/992916 shuffle本意是洗牌的意思.在mapreduce中描述的是怎么将map task 的输出结果有效的传送到reduce tas ...
- Delphi下OpenGL2d绘图(04)-画四边形
一.前言 画四边形基本上与前几遍文字代码是相同.区别在于glBegin()的参数“GL_QUADS”.绘制的框架代码可以使用 Delphi下OpenGL2d绘图(01)-初始化 中的代码.修改的部份为 ...
- C#实现单实例运行
C#实现单实例运行的方法,也有多种,比如利用 Process 查找进程的方式,利用 API findwindow 查找窗体的方式,还有就是 利用 Mutex 原子操作,上面几种方法中, 综合考虑利用 ...
- 移动端的touchstart,touchmove,touchend事件中的获取当前touch位置
前提:touchstart,touchmove,touchend这三个事件可以通过原生和jq绑定. 原生:document.querySelector("#aa").addEven ...
- 深入理解 flex 布局以及计算_Flexbox, Layout
起因 对于Flex布局,阅读了 大漠老师和其他老师写的文章后,我还是不太理解Flexbox是如何弹性的计算子级项目的大小以及一些其他细节.在大漠老师的帮助下,我去查阅Flexbox 的 W3C 规范文 ...
- ASP.Net 之委托事件
1.首先给一张图让大家了解什么是委托?它的优缺点是什么? 2.通过代码的运用更深入地了解委托事件(窗体应用程序) 1)下面我们先定义一个无参数的委托. //1.0 定义一个自定义的委托,此委托的签名是 ...
- poj 1811 Prime Test 大数素数测试+大数因子分解
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 27129 Accepted: 6713 Case ...
- spring与dwr整合实现js直接使用java代码
此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 一. jar包介绍 1. dwr-3.0.1.jar,支持 spring 4.3.4 的最低版本 ...
- HDU 2680(最短路)(多个起始点)
这道题也是死命TLE.. http://acm.hdu.edu.cn/showproblem.php?pid=2680 /* 使用pair代替结构 */ #include <iostream&g ...
- maven 程序包com.sun.image.codec.jpeg
在 Pom.xml 增加 <build> <plugins> <plugin> <artifactId>maven-compiler-plugin< ...