参考链接: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错误的更多相关文章

  1. 「快学springboot」集成Spring Security实现鉴权功能

    Spring Security介绍 Spring Security是Spring全家桶中的处理身份和权限问题的一员.Spring Security可以根据使用者的需要定制相关的角色身份和身份所具有的权 ...

  2. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  3. Spring Boot中集成Spring Security 专题

    check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...

  4. Spring Security 动态url权限控制(三)

    一.前言 本篇文章将讲述Spring Security 动态分配url权限,未登录权限控制,登录过后根据登录用户角色授予访问url权限 基本环境 spring-boot 2.1.8 mybatis-p ...

  5. springboot+maven整合spring security

    springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...

  6. 关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  7. 四:Spring Security 登录使用 JSON 格式数据

    Spring Security 登录使用 JSON 格式数据 1.基本登录方案 1.1 创建 Spring Boot 工程 1.2 添加 Security 配置 2.使用JSON登录 江南一点雨-Sp ...

  8. 页面获取Spring Security登录用户

    1.在session中取得spring security的登录用户名如下:${session.SPRING_SECURITY_CONTEXT.authentication.principal.user ...

  9. Spring Security 登录校验 源码解析

    传统情况下,在过滤器中做权限验证,Spring Secuirty也是在Filter中进行权限验证. 创建并注册过滤器 package com.awizdata.edubank.config; impo ...

随机推荐

  1. 【AtCoder】AGC002

    AGC002 A - Range Product #include <bits/stdc++.h> #define fi first #define se second #define p ...

  2. php 连接webservice接口

    首先谢谢前人, 引用:https://www.cnblogs.com/xbxxf/p/10103430.html 本来说对接接口,我以为是一扮curl接口形式,结果最后给接口锝时候才告诉我是webse ...

  3. 4.Linux系统命令及其使用详解

    运维工程师必会的109个Linux命令   文件管理basename:从文件名中去掉路径和扩展名 cat:把档案串连接后传到基本输出(屏幕或加 > filename 到另一个档案)cd:切换目录 ...

  4. Codeforces 1097E. Egor and an RPG game

    传送门 首先考虑怎么算 $f(n)$ (就是题目里面那个 $f(n)$) 发现可以构造一组序列大概长这样: ${1,3,2,6,5,4,10,9,8,7,15,14,13,12,11,...,n(n+ ...

  5. DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化

    DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化 2017年11月29日 06:40:37 机器之心V 阅读数 2183   版权声明:本文为博主原创文章,遵循CC 4.0 BY ...

  6. mysql 5.7.19安装

    从mysql官网下载的mysql5.7.19免安装版, 安装时出现问题,mysql总是启动不起来,在网上查了下资料,做个记录: .将mysql解压到指定的文件夹 .以管理员身份运行cmd .定位到my ...

  7. mysql5.7 密码字段名更改

    由password更改为authentication_string update user set authentication_string=password("123456") ...

  8. Spark RDD学习笔记

    一.学习Spark RDD RDD是Spark中的核心数据模型,一个RDD代表着一个被分区(partition)的只读数据集. RDD的生成只有两种途径: 一种是来自于内存集合或外部存储系统: 另一种 ...

  9. NLog Helpper日志帮助类配置和使用

    1.帮助类  (首先需要引入NLog.dll) using System; namespace XXXXXX { /// <summary> /// 用法实例 : NLogTest.Nlo ...

  10. win10环境安装配置Nginx

    前言: 参考 https://blog.csdn.net/kisscatforever/article/details/73129270 Nginx的应用场景      1. http服务器.Ngin ...