搭建认证服务器,资源服务器

Sso

即多个站点共用一台认证授权服务器,用户在站点登录后,可以免登录访问其他所有站点

Oauth2 协议 授权第三方进行认证

resource owner:资源所有者,这里可以理解为用户。 security (yyl-123456)

client:客户端,可以理解为一个第三方的应用程序 即微博 CSDN。(oauth admin 123456)

authorization server:(sso)

认证/授权服务器,它认证resource owner的身份,为 resource owner提供授权审批流程,并最终颁发授权令牌(Access Token)。

resource server:资源服务器: 除了认证服务器之外的其他的服务器

微服务项目里面认证服务器只能有一台

资源服务器多台

Oauth2

四个角色:

resource owner:资源所有者,这里可以理解为用户。

client:客户端,可以理解为一个第三方的应用程序 即微博 CSDN。

resource server:资源服务器,它存储用户或其它资源。

authorization server:

认证/授权服务器,它认证resource owner的身份,为 resource owner提供授权审批流程,并最终颁发授权令牌(Access Token)。

认证服务器只有一个 sysauth

资源服务器 订单 商品 支付

四种授权模式

* authorization_code 授权码模式

* password 密码模式

* implicit 简单模式

* client_credentials 客户端模式

目的: 颁发token

获取token 校验token

认证服务器里面的

常用的路径:

/oauth/authoriz

授权端点

/oauth/token

令牌端点获取token

/oauth/confirm_access

用户确认授权提交端点

/oauth/error

授权服务错误信息端点

/oauth/check_token

用于资源服务访问的令牌解析端点

/oauth/token_key

提供公有密匙的端点,如果你使用JWT(RSA)令牌的

搭建认证服务器:

Oauth2

Springsecurity-oauth2

1 创建父项目

2 启动nacos

3 创建授权微服务sys-auth

pom文件:

配置参数

  1. 加jar

<!--oauth2-->

<dependency>

<groupId>org.springframework.security.oauth</groupId>

<artifactId>spring-security-oauth2</artifactId>

<version>2.3.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-openfeign</artifactId>

</dependency>

授权码模式:

  1. 访问授权站点生成一个授权码

localhost:8081 指的是认证服务器所在的地址

/oauth/authorize 授权站点路径(框架提供)

http://localhost:8081/oauth/authorize?response_type=code&client_id=admin&scop=all

跳转成功

授权码:SsZ9rE

  1. 授权码生成token

http://localhost:8081/oauth/token?grant_type=authorization_code&code=SsZ9rE&client_id=admin&redirect_url=http://www.baidu.com&scope=all

简单模式:

其他地方不变

http://localhost:8081/oauth/authorize?response_type=token&client_id=admin&scope=all

https://www.baidu.com/#access_token=5037f7b1-da2f-4068-a857-5b21fa4a8251&token_type=bearer&expires_in=43199

客户端模式:

只修改模式,访问授权码模式路径即可,简单修改模式参数

密码模式:

security的配置文件中进行的配置

  /**
     * 配置密码模式的时候需要这个Bean
     * @return
     * @throws Exception
     */
    @Bean
    public AuthenticationManager getAuthManger() throws Exception {
        return super.authenticationManagerBean();
    }

oauth的配置文件中配置:

      /**
     * 配置凭证的信息
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
    }

不想重复的输入第三方的用户名和密码

放行

 /**
     * 安全配置
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients()
                .checkTokenAccess("permitAll()")//校验token 放行
                .tokenKeyAccess("permitAll()");//获取token的值
    }

无登陆用户和密码(参数中有)成功放行

校验token

localhost:8081/oauth/check_token?token=d72c34f2-0a97-47c8-893a-1b3d7a5e74c8

使用jwt类型的token

Jwt 类型 三段 头类型 sign的加密方式

载荷部分:username 资源的信息 过期的时间

签名部分(自己来写的)

Token

加jar

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-jwt</artifactId>

<version>1.1.0.RELEASE</version>

</dependency>

配置bean

 @Bean
    public TokenStore getTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }
    /**
     * 生成token的bean
     * 解析token的bean
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey("xiaowu");
        return jwtAccessTokenConverter;
    }

使用bean

 /**
     * 配置凭证的信息
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)
                .tokenStore(getTokenStore())//token存储的地方
                .accessTokenConverter(jwtAccessTokenConverter());//生成token的bean  也是  解析token的bean
    }

认证之后直接生成token

安全配置文件中配置认证成功之后使用第三方工具hutool 发出post请求 生成token 返回客户端

配置文件代码

      http.formLogin().permitAll().loginProcessingUrl("/userlogin")
                        .successHandler((httpServletRequest, httpServletResponse, authentication) -> {
                           //httpServletRequest
                            String username = httpServletRequest.getParameter("username");
                            String password = httpServletRequest.getParameter("password");
                           //hutool工具  发出post请求 获取token的值
                            HttpRequest post = HttpUtil.createPost("http://localhost:8081/oauth/token");//路径
                            //参数
                            post.form("grant_type","password");
                            post.form("client_id","admin");
                            post.form("client_secret","123456");
                            post.form("username",username);
                            post.form("password",password);
                                //
                            HttpResponse execute = post.execute();//发送请求
                            String body = execute.body();
                            System.out.println(body);
                            //token的值
                            //字符串转化为map
                            JSONObject entries = JSONUtil.parseObj(body);
                            Object accessToken = entries.get("access_token");
                            // 返回一个token
                            printJsonData(httpServletResponse,new Resout<>(200,"成功",accessToken));
                            //返回一个token

运行成功

资源服务器:

  1. 加jar

<dependency>

<groupId>org.springframework.security.oauth</groupId>

<artifactId>spring-security-oauth2</artifactId>

<version>2.3.5.RELEASE</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.security.oauth.boot/spring-security-oauth2-autoconfigure -->

<dependency>

<groupId>org.springframework.security.oauth.boot</groupId>

<artifactId>spring-security-oauth2-autoconfigure</artifactId>

<version>2.3.5.RELEASE</version>

</dependency>

  1. 编写配置文件
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

@Configuration 
@EnableResourceServer
@EnableGlobalMethodSecurity(jsr250Enabled = true,prePostEnabled = true,securedEnabled=true)
public class MyResConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                //.access("@RbacConfig.hasPermission(request,authentication)")
                .and()
                .csrf().disable();
    }
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey("yyl"); // 
        return jwtAccessTokenConverter;
    }
}
  1. 写正常的逻辑

  1. 测试token是否生效

携带token 访问资源服务器

Authorization 放在headers里面的

Token的值 要求必须是bearer 类型的 token前面加上这个类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Radius认证服务器是一种用于用户认证和授权的网络协议。搭建Radius认证服务器可以提供更高的网络安全性和用户管理便利性。 首先,搭建Radius认证服务器需要选择合适的服务器操作系统,如Linux或Windows Server。然后,在服务器上安装并配置Radius服务器软件,如FreeRADIUS或Microsoft Network Policy Server。 在配置Radius服务器时,需要设置服务器的IP地址、端口号、认证方式和密钥等基本参数。比如,可以选择使用PEAP或EAP-TTLS等加密认证协议,以保证用户的身份安全。 接下来,需要创建Radius服务器上的用户数据库。可以使用本地数据库或连接外部数据库,如MySQL或Active Directory。在数据库中,可以添加和管理用户的账号和密码,并给予不同的权限和认证方式。 在Radius服务器上,还需要配置客户端设备的接入控制策略。可以根据需要,设置不同的认证方式、访问限制和审计日志等。 最后,测试和验证Radius服务器的运行状态。可以使用Radius客户端工具,如NTRadPing或RadTest,进行用户认证和授权测试。确保Radius服务器能够正常对接入的设备和用户进行认证和授权。 总之,搭建Radius认证服务器可以为网络提供更高的安全性和用户管理便利性。正确配置和管理Radius服务器,可以有效地保护网络资源和用户信息的安全。 ### 回答2: Radius(Remote Authentication Dial In User Service)是一种广泛应用于网络认证与授权的协议。搭建Radius认证服务器可以提供强大的身份验证和访问控制功能。以下是Radius认证服务器搭建的简要步骤: 1.选择合适的操作系统:可以选择类Unix操作系统,如Linux或FreeBSD。 2.安装Radius服务器软件:根据所选的操作系统,安装合适的Radius服务器软件,如FreeRADIUS或OpenRADIUS。 3.配置Radius服务器:打开服务器配置文件,通常是radiusd.conf或radius.conf,根据需要进行一些基本配置,如指定监听IP地址、端口号和共享密码等。 4.配置用户数据库:一般会使用数据库管理用户信息,如MySQL或PostgreSQL。在Radius服务器配置文件中,指定数据库服务器的地址、用户名、密码和数据库名称。 5.创建用户账号:在数据库中创建用户账号,包括用户名、密码和所属的组或角色等信息。 6.配置客户端设备:在Radius服务器配置文件中,定义客户端设备的IP地址和共享密码。 7.启动Radius服务器:在命令行界面输入启动命令,启动Radius服务器。 8.测试认证功能:使用客户端设备连接到网络,输入用户名和密码进行认证,验证Radius服务器是否正常工作。 9.进一步配置:根据需求,可以进一步配置Radius服务器,如增加额外的认证方式、配置访问控制策略等。 10.监控和维护:定期监控Radius服务器的运行状态,处理日志和错误报告,及时进行维护和升级操作。 通过以上步骤,可以成功搭建Radius认证服务器,提供可靠的网络认证和授权服务。 ### 回答3: Radius(Remote Authentication Dial-In User Service)是一种用于网络访问身份验证和授权的协议。搭建一个Radius认证服务器需要进行以下步骤: 首先,选择一个适合的操作系统来搭建服务器,常用的有Windows Server和Linux系统。根据选择的操作系统,安装相应的服务器软件,例如Windows Server系统可以安装Internet Authentication Service(IAS)或Network Policy Server(NPS),Linux系统可以安装Freeradius软件。 安装完服务器软件后,配置服务器的基本参数。包括IP地址、端口号等。此外,还需要为服务器生成证书,以确保通信的安全性。 接下来,配置服务器的用户和组。这些用户和组用于认证和授权用户访问网络。可以通过本地数据库、LDAP服务器或其他外部身份验证源来配置用户和组。 在配置用户和组之后,进行身份验证和授权设置。定义认证方法,如用户名/密码、数字证书或其他认证方式。同时,定义授权策略,指定每个用户或用户组可访问的资源和权限。 最后,进行网络设备的配置。将网络设备连接到Radius服务器,并在设备上配置Radius客户端设置。这样,当用户尝试访问网络设备时,设备将向Radius服务器发送身份验证请求,并根据服务器的响应来授权用户访问。 整个过程需要仔细配置服务器和网络设备,确保服务器和设备之间的通信正常。此外,还需要保护服务器的安全,定期更新系统和软件补丁,设置访问控制和日志记录,以避免安全威胁。 以上是Radius认证服务器搭建的基本步骤,但实际操作时可能会有一些差异,具体的步骤和设置取决于所使用的服务器软件和操作系统版本。在搭建过程中,可以参考相关的官方文档或网络教程来进行操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值