百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

分布式系统开发实战:基于Spring Security实现安全认证

yuyutoo 2024-10-26 16:08 6 浏览 0 评论

在本节,将演示基于Spring Security安全认证功能。该应用代码可以在security basic目录下找到。

19.5.1 添加依赖

添加Spring Security的依赖时,由于是使用的snapshot版本,所以,要配置Spring Snapshots仓库。配置如下。

<!-- Spring Snapshots仓库 -->
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
</repository>
</repositories>
<properties>
<spring.version>5.1.5.RELEASE</spring.version>
<jetty.version>9.4.14.v20181114</jetty.version>
<jackson.version>2.9.7</jackson.version>
<spring-security.version>5.2.0.BUILD-SNAPSHOT</spring-security.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency><!-- 安全相关的依赖 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
</dependency>
</dependencies>

19.5.2 添加业务代码

业务代码里面包含了模型和控制器。

1.User模型

User类代码如下。

package com.waylau.spring.mvc.vo;
public class User {
private String username;
private Integer age;
public User(String username, Integer age) {
this.username = username;
this.age = age;
}
//省略getter/setter方法
}

2.控制器

控制器HelloController代码如下。

package com.waylau.spring.mvc.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.waylau.spring.mvc.vo.User;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello World! Welcome to visit waylau.com!";}
@RequestMapping("/hello/way")
public User helloWay() {
return new User("Way Lau", 30);
}
}

上述控制器的逻辑非常简单,当访问“/hello”时,会响应一段文本;当访问“/hello/way”会返回一个POJO对象。

该POJO对象,可以根据消息转换器的设置,来生成不同格式的消息。

19.5.3 配置消息转换器

添加Spring Web MVC的配置类MvcConfiguration,并在该配置中启用消息转换器。

package com.waylau.spring.mvc.configuration;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebMvc // 启用MVC
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
public void extendMessageConverters(List<HttpMessageConverter<?>> cs) {
// 使用Jackson JSON来进行消息转换
cs.add(new MappingJackson2HttpMessageConverter());
}
}

由于预先在pom.xml中添加了Jackson JSON的依赖,因此可以使用

Jackson JSON来进行消息转换,将响应消息体转为JSON格式。

19.5.4 配置Spring Security

以下是针对Spring Security的配置。

package com.waylau.spring.mvc.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import
org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAd
apter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@EnableWebSecurity // 启用Spring Security功能
public class WebSecurityConfig
extends WebSecurityConfigurerAdapter {
/**
* 自定义配置
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated()//所有请求都需认证
.and()
.formLogin() // 使用form表单登录
.and()
.httpBasic(); // HTTP基本认证
}
@SuppressWarnings("deprecation")
@Bean
public UserDetailsService userDetailsService() {
InMemoryUserDetailsManager manager =
new InMemoryUserDetailsManager();
manager.createUser(
User.withDefaultPasswordEncoder() // 密码编码器
.username("waylau") // 用户名
.password("123") // 密码
.roles("USER") // 角色
.build()
);
return manager;
}
}

在上述配置中,要启动Spring Security功能,需要在配置类上添加@EnableWebSecurity注解。

安全配置类WebSecurityConfig继承自

WebSecurityConfigurerAdapter。WebSecurity Configurer Adapter提供用于创建一个Websecurityconfigurer实例方便的基类,允许自定义重写其方法。这里,我们重写了configure方法:authorizeRequests().anyRequest().authenticated()方法意味着所有请求都需认证;formLogin()方法表明这是基于表单的身份验证;httpBasic()方法表明该认证是一个HTTP基本认证。

UserDetailsService用于提供身份认证信息。本例使用了基于内存的信息管理器InMemory UserDetailsManager,同时初始化了一个用户名为“waylau”、密码为“123”、角色为“USER”的身份信息。withDefaultPasswordEncoder()方法指定了该用户身份信息使用默认的密码编码器。

19.5.5 创建应用配置类

AppConfiguration是整个应用的配置类,用于导入Spring Web MVC及Spring Security的配置信息。代码如下。

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan(basePackages = { "com.waylau.spring" })
@Import({ WebSecurityConfig.class, MvcConfiguration.class})
public class AppConfiguration {
}

19.5.6 创建内嵌Jetty的服务器

创建内嵌了Jetty的服务器,代码如下。

package com.waylau.spring.mvc;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;
import com.waylau.spring.mvc.configuration.AppConfiguration;
public class JettyServer {
private static final int DEFAULT_PORT = 8080;
private static final String CONTEXT_PATH = "/";
private static final String MAPPING_URL = "/*";
public void run() throws Exception {
Server server = new Server(DEFAULT_PORT);
server.setHandler(servletContextHandler(webApplicationContext()));
server.start();
server.join();
}
private ServletContextHandler servletContextHandler(WebApplicationContext ct) {
// 启用Session管理器
ServletContextHandler handler =
new ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath(CONTEXT_PATH);
handler.addServlet(new ServletHolder(new DispatcherServlet(ct)),
MAPPING_URL);
handler.addEventListener(new ContextLoaderListener(ct));
// 添加Spring Security过滤器
FilterHolder filterHolder=new FilterHolder(DelegatingFilterProxy.class);
filterHolder.setName("springSecurityFilterChain");
handler.addFilter(filterHolder, MAPPING_URL,
EnumSet.of(DispatcherType.REQUEST));
return handler;
}
private WebApplicationContext webApplicationContext() {
AnnotationConfigWebApplicationContext context =
new AnnotationConfigWebApplicationContext();
context.register(AppConfiguration.class);
return context;
}
}

JettyServer将Spring的上下文Servlet、监听器、过滤器等信息都传给了Jetty服务。

19.5.7 应用启动器

创建应用启动类Application,代码如下。

package com.waylau.spring.mvc;
public class Application {
public static void main(String[] args) throws Exception {
new JettyServer().run();
}
}

19.5.8 运行应用

右键运行Application类即可启动应用。

访问http://localhost:8080/hello/way,会跳转到登录界面,意味着被安全认证拦截了。正如WebSecurityConfig所配置的那样,登录界面是一个form表单,

尝试输入一个错误的用户名和密码,可以看到所示的错误提示信息。


用初始化好的用户名和密码进行成功登录,可以看到能够正常访问应用的API

19.6 本章小结

本章介绍了分布式系统安全性的基本概念、加密算法,同时也介绍了安全通道及访问控制。本章也演示了如何基于Spring Security框架来实现安全认证。

相关推荐

12、高阶组件:魔法增幅器——React 19 HOC模式

一、魔法增幅器的本质"高阶组件是魔法师用咒语叠加的炼金术,"霍格沃茨魔咒研究院院长凝视着发光的增幅器,"通过函数式能量场的嵌套,让基础组件获得预言家日报式的逻辑继承!"...

深入理解nodejs的异步IO与事件模块机制

一、node为什么要使用异步I/O异步最先诞生于操作系统的底层,在底层系统中,异步通过信号量、消息等方式有广泛的应用。但在大多数高级编程语言中,异步并不多见,这是因为编写异步的程序不符合人习惯的思维逻...

前端时间同步利器:React + useEffect 实现高性能动态时钟

前言在你奋笔疾敲代码的瞬间,是不是突然一低头,发现时间像偷偷跑路的变量,一眨眼就从上午飘到下午?饭没吃、会没开、工位也快被前端猫霸占了。仿佛你写的不是代码,而是“时间穿梭机”。别慌,咱们今天就来用R...

JavaScript 异步编程指南 - 聊聊 Node.js 中的事件循环

作者:五月君来源:编程界|事件循环是一种控制应用程序的运行机制,在不同的运行时环境有不同的实现,上一节讲了浏览器中的事件循环,它们有很多相似的地方,也有着各自的特点,本节讨论下Node.js中...

10个Vue开发技巧「实践」

作者:WahFung转发链接:https://juejin.im/post/5e8a9b1ae51d45470720bdfa路由参数解耦一般在组件内使用路由参数,大多数人会这样做:...

通过番计时器实例学习 React 生命周期函数 componentDidMount

大家好,今天我们将通过一个实例——番茄计时器,学习下如何使用函数生命周期的一个重要函数componentDidMount():componentDidMount(),在组件加载完成,render之后...

SRE监控四大黄金指标,任何一个有异常都会是灾难……

导读...

前端必看!10 个 Vue3 救命技巧,解决你 90% 的开发难题?

写Vue3项目时,是不是总被数据更新延迟、组件间传值混乱、页面加载缓慢这些问题折磨得头秃?别担心!作为摸爬滚打多年的老前端,今天掏出压箱底的10个实战技巧,从性能优化到复杂逻辑处理,每一个都能...

如何用2 KB代码实现3D赛车游戏?2kPlus Jam大赛了解一下

选自frankforce作者:Frank机器之心编译参与:王子嘉、GeekAI控制复杂度一直是软件开发的核心问题之一,一代代的计算机从业者纷纷贡献着自己的智慧,试图降低程序的计算复杂度。然而,将一款...

证明你访问的网站是你想访问的,Safari 真的需要

安全研究员在Safari上找到了一个新漏洞,能让网站在浏览器的地址栏内将自己伪装成另一个网站——得益于Safari地址栏的“智能缩略”功能。在Deusen最近公开的攻击演示(PoC,P...

抓狂!TS 组件性能拉胯到崩溃?4 个绝杀技巧逆风翻盘!

前端兄弟姐妹们五一假期快乐,咱们谁还没被TypeScript组件的性能问题折磨过?页面加载转圈圈,点击按钮没反应,代码改了一轮又一轮,性能却还是原地踏步,分分钟想砸电脑!别慌,今天这4个绝杀技...

让小球做圆周运动,你有几种办法?

最近在阅读外国技术文章中无意中发现了一个神奇的CSS属性motion-path,它可以让Dom元素可以按照自定义的路径移动。又想起了很久之前参加校招面试的时候,面试官问了我一个问题“能不能不借助库实现...

前端基础进阶(十四):深入核心,详解事件循环机制

EventLoopJavaScript的学习零散而庞杂,很多时候我们学到了一些东西,但是却没办法感受到进步!甚至过了不久,就把学到的东西给忘了。为了解决自己的这个困扰,在学习的过程中,我一直在试图寻...

从0搭建一个WebRTC,实现多房间多对多通话,并实现屏幕录制

这篇文章开始会实现一个一对一WebRTC和多对多的WebRTC,以及基于屏幕共享的录制。本篇会实现信令和前端部分,信令使用fastity来搭建,前端部分使用Vue3来实现。为什么要使用WebRTCWe...

Vue2 开发卡壳?这 10 个实战技巧专治各种不服

干前端开发的兄弟,谁还没被Vue2折腾过?数据不更新、组件通信乱成麻、性能差到想砸电脑……这些痛点,我都懂!今天直接甩出10个超实用的实战技巧,每一个都是从项目“血坑”里爬出来总结的,专...

取消回复欢迎 发表评论: