分布式系统开发实战:基于Spring Security实现安全认证
yuyutoo 2024-10-26 16:08 10 浏览 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框架来实现安全认证。
相关推荐
- 全局和隐式 using 指令详解(全局命令)
-
1.什么是全局和隐式using?在.NET6及更高版本中,Microsoft引入了...
- 请停止微服务,做好单体的模块化才是王道:Spring Modulith介绍
-
1、介绍模块化单体是一种架构风格,代码是根据模块的概念构成的。对于许多组织而言,模块化单体可能是一个很好的选择。它有助于保持一定程度的独立性,这有助于我们在需要的时候轻松过渡到微服务架构。Spri...
- ASP.NET程序集引用之痛:版本冲突、依赖地狱等解析与实战
-
我是一位多年后端经验的工程师,其中前几年用ASP.NET...
- .NET AOT 详解(.net 6 aot)
-
简介AOT(Ahead-Of-TimeCompilation)是一种将代码直接编译为机器码的技术,与传统的...
- 一款基于Yii2开发的免费商城系统(一款基于yii2开发的免费商城系统是什么)
-
哈喽,我是老鱼,一名致力于在技术道路上的终身学习者、实践者、分享者!...
- asar归档解包(游戏arc文件解包)
-
要学习Electron逆向,首先要有一个Electron开发的程序的发布的包,这里就以其官方的electron-quick-start作为例子来进行一下逆向的过程。...
- 在PyCharm 中免费集成Amazon CodeWhisperer
-
CodeWhisperer是Amazon发布的一款免费的AI编程辅助小工具,可在你的集成开发环境(IDE)中生成实时单行或全函数代码建议,帮助你快速构建软件。简单来说,AmazonCodeWhi...
- 2014年最优秀JavaScript编辑器大盘点
-
1.WebstormWebStorm是一种轻量级的、功能强大的IDE,为Node.js复杂的客户端开发和服务器端开发提供完美的解决方案。WebStorm的智能代码编辑器支持JavaScript,...
- 基于springboot、tio、oauth2.0前端vuede 超轻量级聊天软件分享
-
项目简介:基于JS的超轻量级聊天软件。前端:vue、iview、electron实现的PC桌面版聊天程序,主要适用于私有云项目内部聊天,企业内部管理通讯等功能,主要通讯协议websocket。支持...
- JetBrains Toolbox推出全新产品订阅授权模式
-
捷克知名软件开发公司JetBrains最为人所熟知的产品是Java编程语言开发撰写时所用的集成开发环境IntelliJIDEA,相信很多开发者都有所了解。而近期自2015年11月2日起,JetBr...
- idea最新激活jetbrains-agent.jar包,亲测有效
-
这里分享一个2019.3.3版本的jetbrains-agent.jar,亲测有效,在网上找了很多都不能使用,终于找到一个可以使用的了,这里分享一下具体激活步骤,此方法适用于Jebrains家所有产品...
- CountDownTimer的理解(countdowntomars)
-
CountDownTimer是android开发常用的计时类,按照注释中的说明使用方法如下:kotlin:object:CountDownTimer(30000,1000){...
- 反射为什么性能会很慢?(反射时为什么会越来越长)
-
1.背景前段时间维护一个5、6年前的项目,项目总是在某些功能使用上不尽人意,性能上总是差一些,仔细过了一下代码发现使用了不少封装好的工具类,工具类里面用了好多的反射,反射会影响到执行效率吗?盲猜了一...
- btrace 开源!基于 Systrace 高性能 Trace 工具
-
介绍btrace(又名RheaTrace)是抖音基础技术团队自研的一款高性能AndroidTrace工具,它基于Systrace实现,并针对Systrace不足之处加以改进,核心改进...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- .NET 奇葩问题调试经历之3——使用了grpc通讯类库后,内存一直增长......
- 全局和隐式 using 指令详解(全局命令)
- 请停止微服务,做好单体的模块化才是王道:Spring Modulith介绍
- ASP.NET程序集引用之痛:版本冲突、依赖地狱等解析与实战
- .NET AOT 详解(.net 6 aot)
- 一款基于Yii2开发的免费商城系统(一款基于yii2开发的免费商城系统是什么)
- asar归档解包(游戏arc文件解包)
- 在PyCharm 中免费集成Amazon CodeWhisperer
- 2014年最优秀JavaScript编辑器大盘点
- 基于springboot、tio、oauth2.0前端vuede 超轻量级聊天软件分享
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)