首页 > 其他分享 >SpringBoot的Security和OAuth2的使用

SpringBoot的Security和OAuth2的使用

时间:2024-06-17 17:56:50浏览次数:11  
标签:OAuth2 http SpringBoot spring boot public token Security security

创建项目

先创建一个spring项目。

然后编写pom文件如下,引入spring-boot-starter-security,我这里使用的spring boot是2.4.2,这里使用使用spring-boot-dependencies,在这里就能找到对应的security的包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>app-kiba-security</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>app-kiba-security</name>
    <description>app-kiba-security</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.4.2</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>


    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.kiba.appkibasecurity.AppKibaSecurityApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

然后访问创建项目时默认生成的接口:http://127.0.0.1:8080/user/123/roles/222,得到如下界面。

image

这是相当于,在我们的接口请求的前面做了一个拦截,类似filter,拦截后,跳转到了一个界面,让我们输入账号密码。这里,我由于没有设置账号密码,所以登录不进去。

设置访问一

下面设置一个账号密码,并且设置hello接口可以直接访问,设置很简单,就是注入两个bean,InMemoryUserDetailsManager和WebSecurityCustomizer,代码如下:

@Configuration
public class SecurityConfig   {
    /**
     * 注册用户,这里用户是在内存中的
     *  {noop}表示“无操作”(No Operation)密码编码。
     * @return
     */
    @Bean
    UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager users = new InMemoryUserDetailsManager();
        users.createUser(User.withUsername("kiba").password("{noop}123").roles("admin").build()); 
        return users;
    }

    /**
     * 让hello可以不用登录,就可以直接访问,例如:http://127.0.0.1:8080/hello?name=kiba就可以直接访问
     * @return
     */
    @Bean
    WebSecurityCustomizer webSecurityCustomizer() {
        return new WebSecurityCustomizer() {
            @Override
            public void customize(WebSecurity web) {
                web.ignoring().antMatchers("/hello");
            }
        };
    }

}

现在我们访问http://127.0.0.1:8080/user/123/roles/222,进入到登录页面,输入kiba/123就可以查看接口执行的结果了。

http://127.0.0.1:8080/hello?name=kiba就无需登录,可以直接访问。

登录一次,其他接口就可以自由访问了

控制请求

现在,增加一个类SecurityAdapter,继承自WebSecurityConfigurerAdapter。然后重写他的configure方法

@Configuration
@AllArgsConstructor
public class SecurityAdapter extends WebSecurityConfigurerAdapter {

    /**
     * authenticated():用户需要通过用户名/密码登录,记住我功能也可以(remember-me)。
     * fullyAuthenticated()用户需要通过用户名/密码登录,记住我功能不行。 
     */
    @Override
    @SneakyThrows
    protected void configure(HttpSecurity http) {
        http.httpBasic().and()
                //禁用跨站请求伪造(CSRF)保护。
                .csrf().disable()
                .authorizeRequests().anyRequest().fullyAuthenticated();
    } 

}

当使用,增加了SecurityAdapter后,我们重新请求http://127.0.0.1:8080/user/123/roles/222,得到界面如下:

image

可以看到,登录界面的样式被美化了。

设置访问二(推荐)

我们还可以使用第二种方法,来做用户密码的配置。

通过重写configure(AuthenticationManagerBuilder auth)函数,来创建用户,这种方式创建用户会将前面的bean-UserDetailsService给覆盖,即,用户只剩下这里创建的。

代码如下:

@Configuration
@AllArgsConstructor
public class SecurityAdapter extends WebSecurityConfigurerAdapter {

    /**
     * authenticated():用户需要通过用户名/密码登录,记住我功能也可以(remember-me)。
     * fullyAuthenticated()用户需要通过用户名/密码登录,记住我功能不行。
     */
    @Override
    @SneakyThrows
    protected void configure(HttpSecurity http) {
        http.httpBasic().and()
                //禁用跨站请求伪造(CSRF)保护。
                .csrf().disable()
                .authorizeRequests().anyRequest().fullyAuthenticated();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("kiba518")
                .password(passwordEncoder().encode("123"))
                .authorities(new ArrayList<>(0));
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}

这里的用户是写死的,用户是可以修改成读取数据库的信息的。

我们查看WebSecurityConfigurerAdapter的代码,可以看到他有注解@Order(100),数越大,执行越优先级越低,即,他的执行顺序是相对比较靠后的。

授权OAuth2

授权这个设计理念是这样,它是结合上面的security的操作,实现了一个普通的WebApp转换成授权服务器WebApp。

授权服务器转换思路

我们先了解一下security转授权服务器的思路。

1,在这个应用里,创建一个auth接口,然后任何人想访问这个接口,就都需要输入账户密码了。

2,我们这个auth接口的返回值是个code,然后我们的前端,或者其他调用接口的APP,就可以把这个code作为用户登录的token了,。

3,然后我们再做一个接口,接受一个token参数,可以验证token是否有效。

这样我们这个授权服务器的搭建思路就构建完成了。

但按这个思路,我们需要做很多操作,比如创建接口,缓存token等等,现在spring提供了一个Oauth2的包,他可以帮我们实现这些接口定义。

OAuth2的接口如下,可以自行研究。

/oauth/authorize:授权端点

/oauth/token:获取令牌端点

/oauth/confirm_access:用户确认授权提交端点

/oauth/error:授权服务错误信息端点

/oauth/check_token:用于资源服务访问的令牌解析端点

/oauth/token_key:提供公有密匙的端点,如果使用JWT令牌的话

实现授权服务器

现在我们实现一个授权服务器。

先添加OAuth2的引用。

 <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.4.0.RELEASE</version>
        </dependency>

然后增加配置文件AuthorizationConfig。

@Configuration
@EnableAuthorizationServer //开启授权服务
public class AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //允许表单提交
        security.allowFormAuthenticationForClients()
                .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("client-kiba") //客户端唯一标识(client_id)
                .secret(passwordEncoder.encode("kiba518-123456")) //客户端的密码(client_secret),这里的密码应该是加密后的
                .authorizedGrantTypes("password") //授权模式标识,共4种模式[授权码(authorization-code)隐藏式(implicit) 密码式(password)客户端凭证(client credentials)]
                .scopes("read_scope"); //作用域

    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
    }

}

然后打开SecurityAdapter,增加一个bean,如下,目的是让上面的AuthorizationConfig里Autowired的authenticationManager可以实例化。

@Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

然后使用APIFox调用一下/oauth/token接口。

先选择auth,输入账号密码,这个账号密码就是AuthorizationConfig里配置的客户端id和密码。

image

这个数据在请求时,会进行base64编码,然后以http的header属性Authorization的值的模式传递,如下。

image

然后输入参数,参数里scope和grant_type要和AuthorizationConfig里定义的scopes和authorizedGrantTypes一样,如下。

image

请求后,得到结果,如上图。

我们得到"access_token": "19d37af2-6e13-49c3-bf19-30a738b56886"。

有了access_token后,我们的前端其实就已经可以进行各种骚操作了。

资源服务

这个是Oauth为我们提供的一项很好用的功能。

我们创建一个项目做为资源服务。

添加依赖,版本与上面相同。

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.4.0.RELEASE</version>
        </dependency>

然后编写资源配置,代码如下:

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Bean
    public RemoteTokenServices remoteTokenServices() {
        final RemoteTokenServices tokenServices = new RemoteTokenServices();
        tokenServices.setClientId("client-kiba");
        tokenServices.setClientSecret("kiba518-123456");
        tokenServices.setCheckTokenEndpointUrl("http://localhost:8080/oauth/check_token");//这个接口是oauth自带的
        return tokenServices;
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.stateless(true);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        //session创建策略
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
        //所有请求需要认证
        http.authorizeRequests().anyRequest().authenticated();
    }
}

因为添加了spring-boot-starter-security,所以,我们请求这个资源WebApp,就都需要输入账号密码。

但因为,我们配置了ResourceServerConfig,这里我们配置了远程token服务,设置的信息是我们上面创建授权服务的信息。所以,在访问这个WebApp时,我们提供token即可。

使用APIFOX测试,先添加auth的token,内容是来自于上面,/oauth/token的返回值access_token的值。

image

然后请求user接口,我这user接口没有参数,请求结果如下:

image

总结

这个授权服务挺好用的,就是配置太繁琐了,初学者不太好理解,而且功能太多,配置太闹心。

这个资源服务还是很贴心的,他提我们实现了,tokencheck的部分,但要注意的是,他这tokencheck是基于http请求的。

虽然Oath很好用,但,我还是觉得,这个认证部分自己写比较好,我们可以根据项目的需求,设计轻量级的授权认证。

比如,我们想减少http请求,把部分tokencheck在缓存内进行check,那使用oauth时,修改起来就会很头疼。如果是自己写的授权服务器,就不会有修改困难的问题。


注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!



若您觉得这篇文章还不错,请点击下方的【推荐】,非常感谢!

https://www.cnblogs.com/kiba/p/18252859

标签:OAuth2,http,SpringBoot,spring,boot,public,token,Security,security
From: https://www.cnblogs.com/kiba/p/18252859

相关文章

  • 基于springboot的南门桥社区疫情防疫系统-48138(免费领源码+数据库)可做计算机毕业设计J
    Springboot南门桥社区疫情防疫系统的设计与实现摘 要信息化社会内需要与之针对性的信息获取途径,但是途径的扩展基本上为人们所努力的方向,由于站在的角度存在偏差,人们经常能够获得不同类型信息,这也是技术最为难以攻克的课题。针对南门桥社区疫情防疫系统等问题,对南门桥社区......
  • Docker+Jenkins+Pipline实现SpringBoot项目input选择不同差异性yml文件打包、执行sh打
    场景Docker+Jenkins+Pipline如何获取git插件环境变量(提交sha、分支等)以及Jenkinsfile中获取sh执行结果(获取git最近提交信息):https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/139697004在上面的基础上实现了使用Jenkinsfile文件获取git的提交记录以及获取sh的执......
  • SpringBoot基础篇
    SpringBoot视频链接:【黑马程序员SpringBoot3+Vue3全套视频教程,springboot+vue企业级全栈开发从基础、实战到面试一套通关】https://www.bilibili.com/video/BV14z4y1N7pg?p=20&vd_source=12bdb5b78bd5d1c45cab173f3aad839b概述SpringBoot是Spring提供的一个子项目......
  • SpringBoot开发Activiti工作流实现审批流程(全套源码)
    前言activiti工作流引擎项目,企业erp、oa、hr、crm等企事业办公系统轻松落地,一套完整并且实际运用在多套项目中的案例,满足日常业务流程审批需求。一、项目形式springboot+vue+activiti集成了activiti在线编辑器,流行的前后端分离部署开发模式,快速开发平台,可插拔工作流服务。工......
  • SpringBoot配置第三方专业缓存技术Memcached 下载 安装 整合测试 5000字详解
    Memcached下载和安装是一个国内使用量还是比较大的技术打开文件夹我们需要在命令行窗口启动注意要以管理员方式运行先尝试进入指定文件然后又再次运行下载memcached.exe-dinstall启动memcached.exe-dstart停止memcached.exe-dstopmemcached.exe-din......
  • CAS单点登录-开启OAuth2.0协议
    1.添加依赖<dependency><groupId>org.apereo.cas</groupId><artifactId>cas-server-support-oauth-webflow</artifactId><version>${cas.version}</version></dependency>2.application.properties添加以下属性###......
  • OAuth2.0 实现单点登录(四种授权方式)
    一、四种授权模式1、客户端模式(ClientCredentials)指客户端以自己的名义,而不是以用户的名,向“服务提供商”进行认证。严格的说,客户端模式并不属于OAuth框架所要解决的问题。在这种模式中,用户直接向客户端注册,客户端以自己的名义要求“服务提供商”提供服务,其实不存在授权问题。流......
  • 使用SpringBoot对接Kafka
    Kafka是什么,以及如何使用SpringBoot对接Kafka一、Kafka与流处理我们先来看看比较正式的介绍:Kafka是一种流处理平台,由LinkedIn公司创建,现在是Apache下的开源项目。Kafka通过发布/订阅机制实现消息的异步传输和处理。它具有高吞吐量、低延迟、可伸缩性和可靠性等优点,使其成为......
  • 基于springboot的球队训练信息管理系统源码数据库
    传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安装球队训练信息管理系统软件来发挥其高效地信息处理的作用,可以规范信息管理流程,让管理工作可以系统化和程序化,同时,球队训练信息管理系......
  • 基于springboot的青年公寓服务平台源码数据库
    传统信息的管理大部分依赖于管理人员的手工登记与管理,然而,随着近些年信息技术的迅猛发展,让许多比较老套的信息管理模式进行了更新迭代,房屋信息因为其管理内容繁杂,管理数量繁多导致手工进行处理不能满足广大用户的需求,因此就应运而生出相应的青年公寓服务平台。本青年公寓服务......