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();

原文地址:https://www.cnblogs.com/it-deepinmind/p/12074325.html

时间: 2024-07-31 02:01:10

springboot中使用spring security,登录url就出现403错误的相关文章

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

一.前言 本篇文章将讲述Spring Security 动态分配url权限,未登录权限控制,登录过后根据登录用户角色授予访问url权限 基本环境 spring-boot 2.1.8 mybatis-plus 2.2.0 mysql 数据库 maven项目 Spring Security入门学习可参考之前文章: SpringBoot集成Spring Security入门体验(一)https://blog.csdn.net/qq_38225558/article/details/101754743

页面获取Spring Security登录用户

页面获取Spring Security登录用户 1.在session中取得spring security的登录用户名如下:${session.SPRING_SECURITY_CONTEXT.authentication.principal.username} spring security 把SPRING_SECURITY_CONTEXT 放入了session 没有直接把username 放进去.下面一段代码主要描述的是session中的存的变量, 存跳转时候的URLsession {SPRIN

SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法

首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml配置文件.我现在使用的是spring boot ,没有了xml文件配置就方便多了.我同样尝试了两种方式,也都是简单的查询,需要更复杂的查询,还需要我研究研究.往下看,需要先配置springboot的开发环境,需要大致了解springboot,这里可以看下面两篇文章: springboot 项目新建 springboot

Spring Security 入门(1-9)Spring Security - 拦截 url

intercept-url配置 通过 pattern属性 指定拦截的 url 通过access属性指定url的访问权限 其中,access 的值是一个字符串,其可以直接是一个权限的定义,也可以是一个表达式. <security:http use-expressions="true">     <security:form-login />     <security:logout />     <security:intercept-url p

记录一下在SpringBoot中实现简单的登录认证

代码参考博客: https://blog.csdn.net/weixin_37891479/article/details/79527641 在做学校的课设的时候,发现了安全的问题,就不怀好意的用户有可能跳过登录直接访问系统的界面和使用里面的功能,于是想为系统加个安全验证.现在常用的安全框架我知道的就是Shiro还有SpringSecurity,但这次我不打算用框架,采用拦截器filter和session技术实现了一个基于session的登录认证. 先简单地说一下原理: session和cook

Spring security用户URL权限之FilterSecurityInterceptor

总: 用户通过浏览器发送URL地址,由FilterSecurityInterceptor判断是否具有相应的访问权限. 对于用户请求的方法权限,例如注解@PreAuthorize("hasRole('ADMIN')"),由MethodSecurityInterceptor判断 两个拦截器都继承了AbstractSecurityInterceptor /* * Copyright 2004, 2005, 2006 Acegi Technology Pty Limited * * Licen

SpringBoot中利用过滤器修改请求url地址

要求: 代码中配置的url路径为http://127.0.0.1/api/associates/queryAssociatesInfo 现在要求http://127.0.0.1/associates/queryAssociatesInfo也可以同样访问同一个conroller下面的method,并且要求参数全部跟随 代码: package com.shitou.huishi.framework.filter; import java.io.IOException; import javax.se

spring security登录人数限制并且同一个账号可以踢掉前一个用户配置文件

一.限制用户登录数和session自动托管 1.maximumSessions:限制登录人数 2.exceptionIfMaximumExceeded: 为true同一账户只能登录一次, 为false同一账户可以登录多次如果配置了org.springframework.security.web.session.ConcurrentSessionFilter则会踢出前一个登录的session 3.sessionRegistry配置session管理 4.concurrentSessionFilte

SpringBoot 集成Spring security

Spring security作为一种安全框架,使用简单,能够很轻松的集成到springboot项目中,下面讲一下如何在SpringBoot中集成Spring Security.使用gradle项目管理工具. 准备数据, CREATE TABLE `user` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `r