Spring Security集成Oauth2实现用户身份验证+授权

本文介绍了如何在Spring Boot项目中集成Spring Security和Oauth2,实现用户身份验证和授权机制。详细步骤包括Spring Boot集成Spring Security、配置服务器、密码加密、授权服务器和资源服务器的配置,并简要概述了Spring Security和Oauth2的基本原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

最近的项目有使用到Sping Security和Oauth做用户身份验证和授权机制,所以在网上找了点资料简单学习并写了个小demo,在此进行记录总结。授权这块是直接拷贝了参考链接中的小demo,后续涉及到第三方应用授权码方式授权的话再学习然后总结

参考链接

1. Spring Security了解:https://www.springcloud.cc/spring-security-zhcn.html

2. Spring Boot集成Spring Security:

https://www.jianshu.com/p/afe6619d9663

https://www.cnblogs.com/lenve/p/11242055.html

https://blog.youkuaiyun.com/qq_29580525/article/details/79317969

https://blog.youkuaiyun.com/yuanlaijike/article/details/80249235

https://blog.youkuaiyun.com/yuanlaijike/article/details/84703690

3. Oauth2了解:

http://www.ruanyifeng.com/blog/2019/04/oauth_design.html

http://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html

https://www.cnblogs.com/meibaorui/p/9182660.html

4. Spring Security集成Oauth2:

https://www.cnblogs.com/fernfei/p/12200226.html

https://blog.youkuaiyun.com/weixin_34080571/article/details/91715402

https://www.cnblogs.com/dalianpai/p/12425962.html

实现过程

1. Spring Boot集成Spring Security

1.1 导入依赖以及配置服务器相关属性

1.1.1 导入依赖

可直接建立Spring Boot项目选择Spring Security,或者也可以建立一个空的Spring Boot项目后在pom.xml依赖配置文件中加入相关依赖。

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>

1.1.2 配置服务器相关属性

一般配置服务器的访问端口和初始路径,默认为8080以及/,配置在resources/application.properties中配置

#配置服务器端口号
server.port=80
#配置服务器入口网址
server.servlet.context-path=/spring

1.2 普通web访问实现(UserController)

这里是写了个登录、home页面,但是由于没有写相应的前端页面,所以当用户跳转/login页面时会显示404错误,以及用户验证成功后跳转的home页面也会显示404错误。此处说明一下实际中会根据需要在SecurityConfig中对页面是否拦截以及跳转情况进行统一规范设计,此demo中直接使用Spring Security默认的login表单,用户身份信息验证成功后会将信息直接打印输出到前端。

@RestController()
@RequestMapping("/user")
public class UserController {

    @PostMapping("login")
    public String login(){
        return "login.html";
    }

    //由于没有写对应的静态页面,所以用户身份验证成功后访问这些页面会报404错误;
    // 如果身份未验证成功会跳转login页面(自己手动写的/Spring Security默认的login表单)
    @GetMapping("home")
    public String home(){
        return "home.html";
    }

    /**
     * 返回当前登录的用户信息(存在SecurityContextHolder的全局变量中)
     * @return
     */
    @GetMapping("/whoim")
    //@PreAuthorize("hasRole('ROLE_USER')") //@PreAuthorize 用于判断用户是否有指定权限,没有就不能访问
    public Object whoIm(){
        return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

1.3 集成Spring Security(SecurityConfig)使用Java内存用户名和密码(指定可登录/通过拦截的用户名和密码,其中密码为明文)

SecurityConfig类继承WebSecurityConfigurerAdapter类,主要实现几个config方法,用来进行接口请求拦截/用户身份验证/前端静态页面拦截

/**
 * 实现一个WebSecurityConfigurerAdapter然后重写一下configure方法,配置登录验证页、请求权限设置
 * 标识该类是配置类、开启 Security 服务、开启全局 Securtiy 注解
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) //启用基于注解的安全性 prePostEnable表示使用基于表达书的语法
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    ObjectMapper objectMapper; //json转换

    /**
     * 接口请求拦截
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests() //安全请求策略,方法可有多个子节点matcher,每个matcher按照其声明顺序执行
                //页面访问权限控制说明
                .antMatchers("/").permitAll() //允许任何用户访问/路径(这个路径得有页面,否则后报404错误)
                .antMatchers("/admin/**").hasRole("ADMIN") //以/admin/开头的URL只能由拥有ROLE_ADMIN角色的用户访问
                .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") //同理以/db/开头的URL
                // 只能由同时具有ROLE_ADMIN和ROLE_DBA的用户访问
                .anyRequest()
//                .access("权限认证bean") 必须经过权限认证以后才能访问
                .authenticated() //其它请求进行拦截(需要进行认证)
                .and()
            .formLogin() //支持基于表单的登录验证
//                .loginPage("../../resources/static/login") //指定登录的路径(用户需要登录验证时跳转login页面)
//                .loginPage("/user/login") //指定跳转controller层的相关方法中
//                .loginProcessingUrl("/index")  //登录成功后跳转/index页面(拦截通过后)
                .permitAll() //允许基于表单登录的所有
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值