springboot中使用spring security,登录url就出现403错误
参考链接:https://segmentfault.com/q/1010000012743613
有两个controller,一个是所有用户可以访问的@RequestMapping("user"),
还有一个是管理员可以访问的@RequestMapping("admin")。
/user/login是UserController中的登录url。
所有操作(除登录注销)都要登录之后才能进行。
现在想用springboot结合spring security实现权限管理。
系统是前后端分离的,controller中返回数据,不返回页面,WebMvcConfig也没有配置什么。
但/user/login,post怎么也不通。报403错误。
这是错误信息

这是WebSecurityConfig
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean
UserDetailsService customUserService() { //注册UserDetailsService 的bean
return new CustomUserService();
} @Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/**").access("hasRole('ROLE_USER')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.anyRequest().authenticated().and() // access after login
// .rememberMe().tokenValiditySeconds(60 * 60 * 24 * 7).key("").and()
.formLogin().loginProcessingUrl("user/login").permitAll().and()
.logout().permitAll();
} @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserService());
}
}
这是CustomUserService
@Service
public class CustomUserService implements UserDetailsService { @Autowired
private UserService userService; @Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
System.out.println("loadUser " + s);
User user = userService.getUserByUsername(s);
if (user == null || user.getIsDel() == 1) {
throw new UsernameNotFoundException("user not exist");
}
List<GrantedAuthority> auths = new ArrayList<>();
for (Role role : user.getRoles()) {
auths.add(new SimpleGrantedAuthority(role.getName())); //不同用户会返回不同的role.name:ROLE_USER, ROLE_ADMIN
}
return new org.springframework.security.core.userdetails.User(s , user.getPwd(), auths);
}
}
最后即使我在WebSecurity中什么也不配置,默认应该是不需要验证session吧。
仍然不行。
protected void configure(HttpSecurity http) throws Exception {
}
问题原因及解决方案:
看错误提示,可能是因为你开启了CSRF保护,关闭即可,在configure(HttpSecurity http)方法中追加http.csrf().disable();

springboot中使用spring security,登录url就出现403错误的更多相关文章
- 「快学springboot」集成Spring Security实现鉴权功能
Spring Security介绍 Spring Security是Spring全家桶中的处理身份和权限问题的一员.Spring Security可以根据使用者的需要定制相关的角色身份和身份所具有的权 ...
- 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- Spring Boot中集成Spring Security 专题
check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...
- Spring Security 动态url权限控制(三)
一.前言 本篇文章将讲述Spring Security 动态分配url权限,未登录权限控制,登录过后根据登录用户角色授予访问url权限 基本环境 spring-boot 2.1.8 mybatis-p ...
- springboot+maven整合spring security
springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...
- 关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- 四:Spring Security 登录使用 JSON 格式数据
Spring Security 登录使用 JSON 格式数据 1.基本登录方案 1.1 创建 Spring Boot 工程 1.2 添加 Security 配置 2.使用JSON登录 江南一点雨-Sp ...
- 页面获取Spring Security登录用户
1.在session中取得spring security的登录用户名如下:${session.SPRING_SECURITY_CONTEXT.authentication.principal.user ...
- Spring Security 登录校验 源码解析
传统情况下,在过滤器中做权限验证,Spring Secuirty也是在Filter中进行权限验证. 创建并注册过滤器 package com.awizdata.edubank.config; impo ...
随机推荐
- 打印 request 请求中的参数
@SuppressWarnings({"rawtypes"})private void showParams(HttpServletRequest request) { Map&l ...
- 小菜鸟之java基础
1. javac 命令的作用: javac 编译器解析 Java 源代码,并生成字节码文件的过程 2. java为什么可以跨平台: ava有虚拟机(JVM),JAVA程序不是直接在电脑上运行的,是在虚 ...
- html中'disabled'与'readonly'的区别
html中'disabled'与'readonly'的区别 此随笔增量编辑 disabled 在提交表单的时候 值不会带入表单中, 而readonly则可以将值带入表单中.
- Centos7 添加开机启动服务
1.在/usr/lib/systemd/system/下创建服务脚本xxx.service,格式如下: [Unit] Description=Scrapyd After=syslog.target n ...
- base64转码java版
package com.net.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.i ...
- 第1章:Python语言与Linux系统管理
1.Python语言为什么流行 1).简单易学 2).丰富强大的库 3).开发效率高 2.Python语言有什么缺点 1).Python的执行速度不够快 2).Python的GIL锁限制并发:GIL是 ...
- MySQL 索引的优化
一.MySQL如何使用索引(index) 1.1 索引概述 索引用于快速查找具有特定列值的行. 如果不使用索引,MySQL必须从表的第一行开始,然后扫描整个表来寻找符合条件的行.这种情况下,表越大,扫 ...
- HTML form表单中action的正确写法
我的Java Web Application的context是myweb,即http://localhost:8080/myweb/index.jsp是欢迎页. 现在我的一个Controller的映射 ...
- 怎样初始化XMLHttpRequest实例对象xhr
xhr.open() 接收5个参数, 用于初始化一个http请求, 它接收5个参数: 1. method: 请求类型; 2. url: 请求的url; 3. async: 是否为异步, 默认为true ...
- VisualStudio2015 安装
环境:Win10 64位 推荐安装顺序 IIS > Sqlserver > Asp.Net 启动安装程序(出现Logo后需要等待1到2分钟),选择安装路径(注意不要出现中文路径) 勾选需求 ...