- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>securitydemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>securitydemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.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.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/min/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.redis.host=192.168.244.136
spring.redis.port=6379
spring.redis.password=root123
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`description` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES (1, 'ROLE_db', '数据库管理员');
INSERT INTO `role` VALUES (2, 'ROLE_admin', '系统管理员');
INSERT INTO `role` VALUES (3, 'ROLE_user', '用户');
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`password` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`enabled` tinyint(1) NULL DEFAULT NULL,
`locked` tinyint(1) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'root', '$2a$10$O8G0X/sUPAA76MV7U3BwY.3Uo8/QMBcqK678Rwkoz.fowbce.CLtO', 1, 0);
INSERT INTO `user` VALUES (2, 'admin', '$2a$10$O8G0X/sUPAA76MV7U3BwY.3Uo8/QMBcqK678Rwkoz.fowbce.CLtO', 1, 0);
INSERT INTO `user` VALUES (3, 'tom', '$2a$10$O8G0X/sUPAA76MV7U3BwY.3Uo8/QMBcqK678Rwkoz.fowbce.CLtO', 1, 0);
-- ----------------------------
-- Table structure for user_role_ref
-- ----------------------------
DROP TABLE IF EXISTS `user_role_ref`;
CREATE TABLE `user_role_ref` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NULL DEFAULT NULL,
`role_id` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user_role_ref
-- ----------------------------
INSERT INTO `user_role_ref` VALUES (1, 1, 1);
INSERT INTO `user_role_ref` VALUES (2, 1, 2);
INSERT INTO `user_role_ref` VALUES (3, 2, 2);
INSERT INTO `user_role_ref` VALUES (4, 3, 3);
SET FOREIGN_KEY_CHECKS = 1;
package com.yl.securitydemo.model;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
public class User implements UserDetails {
private Integer id;
private String username;
private String password;
private Boolean enabled;
private Boolean locked;
private List<Role> roles;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<SimpleGrantedAuthority> list = new ArrayList<>();
for (Role role : roles) {
list.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));
}
return list;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
// 账户是否未过期
@Override
public boolean isAccountNonExpired() {
return true;
}
// 账户是否未锁定
@Override
public boolean isAccountNonLocked() {
return !locked;
}
// 凭证是否未过期
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return enabled;
}
public void setPassword(String password) {
this.password = password;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public void setLocked(Boolean locked) {
this.locked = locked;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return username.equals(user.username);
}
@Override
public int hashCode() {
return Objects.hash(username);
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", enabled=" + enabled +
", locked=" + locked +
'}';
}
}
package com.yl.securitydemo.model;
import java.io.Serializable;
public class Role implements Serializable {
private Integer id;
private String name;
private String description;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Role{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
package com.yl.securitydemo.model;
import java.io.Serializable;
public class UserRoleRef implements Serializable {
private Integer id;
private Integer userId;
private Integer roleId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
@Override
public String toString() {
return "UserRoleRef{" +
"id=" + id +
", userId=" + userId +
", roleId=" + roleId +
'}';
}
}
package com.yl.securitydemo.mapper;
import com.yl.securitydemo.model.Role;
import com.yl.securitydemo.model.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
User loadUserByUsername(String username);
List<Role> getRolesByUserId(Integer userId);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yl.securitydemo.mapper.UserMapper">
<select id="loadUserByUsername" resultType="com.yl.securitydemo.model.User">
select * from user where username = #{username}
</select>
<select id="getRolesByUserId" resultType="com.yl.securitydemo.model.Role">
select * from role where id in (select role_id from user_role_ref where user_id = #{userId})
</select>
</mapper>
package com.yl.securitydemo.service;
import com.yl.securitydemo.mapper.UserMapper;
import com.yl.securitydemo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class UserService implements UserDetailsService {
@Autowired
UserMapper userMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userMapper.loadUserByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("用户名不存在");
}
user.setRoles(userMapper.getRolesByUserId(user.getId()));
return user;
}
}
package com.yl.securitydemo.config;
import com.yl.securitydemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.session.HttpSessionEventPublisher;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.security.SpringSessionBackedSessionRegistry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserService userService;
@Autowired
FindByIndexNameSessionRepository sessionRepository;
// @Autowired
// MyWebAuthenticationDetailsSource myWebAuthenticationDetailsSource;
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// @Bean
// MyAuthenticationProvider myAuthenticationProvider() {
// MyAuthenticationProvider provider = new MyAuthenticationProvider();
// provider.setPasswordEncoder(passwordEncoder());
// provider.setUserDetailsService(userService);
// return provider;
// }
@Bean
SpringSessionBackedSessionRegistry sessionRegistry() {
return new SpringSessionBackedSessionRegistry(sessionRepository);
}
// @Bean
// HttpSessionEventPublisher httpSessionEventPublisher() {
// return new HttpSessionEventPublisher();
// }
// @Override
// @Bean
// protected AuthenticationManager authenticationManager() throws Exception {
// return new ProviderManager(myAuthenticationProvider());
// }
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
// .antMatchers("/getVerifyCode")
// .permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
// .authenticationDetailsSource(myWebAuthenticationDetailsSource)
.permitAll()
.and()
.csrf().disable()
.sessionManagement()
.maximumSessions(1) //限制能登录的用户个数为1
.maxSessionsPreventsLogin(true)// 禁止后面的用户登录
.sessionRegistry(sessionRegistry());
}
}
package com.yl.securitydemo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
@Value("${server.port}")
Integer port;
@GetMapping("/get")
public String get(HttpSession session) {
return session.getAttribute("name") + ":" + port;
}
@GetMapping("/set")
public String set(HttpSession session){
session.setAttribute("name","yl");
return String.valueOf(port);
}
}
1.打包项目,并且以8080和8081两个端口号启动两个实例
2.在客户端A登录admin账号,并且访问8080端口的hello接口
3.在客户端B再登录admin账号,访问8081端口的hello接口,登录发现账号已经登录了,成功限制用户的登录个数为1
由于 PostgreSQL 人员选择的术语不当,这会让一些人感到困惑,但请耐心等待... 我们需要能够支持多个 PostgreSQL (PG) 集群,并将它们集群在多个服务器上,例如使用repmgr.
Hadoop会跑很多jobs,从Hbase读取数据,写数据到数据库。假设我有 100 个节点,那么有两种方法可以构建我的 Hadoop/Hbase集群: 100节点hadoop&hbase集群(1个b
在kafka中,我们能创建多种类型的集群,一般如下: 单节点——单个broker集群 单节点——多broker集群 多结点——多broker集群 kafka集群中主要有五个组件: Topic:主题主要
我想在两台机器上运行我的 MPI 程序,一台是 ubuntu 18.04,另一台是 Windows 10。是否可以使用不同的操作系统组成一个集群? (我正在使用 MPICH) 如果可能,怎么做?我在网
简介: 更新负载平衡集群中的节点的最佳实践是什么? 我们在 ha 代理负载均衡器后面使用 RabbitMQ 集群来支持我们的客户端轻松集群,as suggested in the RabbitMQ d
我正在尝试创建一个图表,我明确指定了许多节点的等级并指定了集群。以下代码生成我想要的图形类型: digraph { rankdir=LR subgraph cluster0 {
我正在尝试在 RABBITMQ 上进行集群。我添加了 2 个节点,但无法添加第 3 个节点。我已经聚集了 rabbit@node1 和 rabbit@node2。现在我正在尝试将 rabbit@nod
我在 MS Azure 的生产环境中启用了一个双集群多区域 HA。 我被要求重用同一个集群来使用微服务管理几个新项目。 这里的最佳做法是什么?我应该为每个应用程序创建一个集群吗?将不同集群中的每个项目
我正在尝试 flex 搜索,并且很难在具有3个 flex 搜索实例的单台计算机上创建集群。我对实例进行了以下更改: 在所有实例上更改了群集名称{cluster.name:es-stack} 在所有实例
我想创建一个redis集群,仅将特定的redis主机作为主服务器和从服务器。另外,我想自己指定哪个主机应该是哪个主机的副本。 目前,cluster-create command当在任何一个 Redis
我计划在具有负载平衡的集群中设置 Magento,那么是否可以让两个 Magento 安装指向同一个数据库? 如果可能的话 - 我应该如何配置这些服务器的基本 URL?服务器应该具有相同的名称吗? 最
我目前正在计划一个 Web 应用程序,我想计划它最终在集群上运行。 集群将由一个 php web 集群和一个 mysql 集群以及一个独立的存储单元组成(也许是一个集群,我真的不知道它是如何工作的:s
我已经安装了 elasticsearch 2.2.3 并在 2 个节点的集群中配置 节点 1 (elasticsearch.yml) cluster.name: my-cluster node.nam
我正在寻找现有的中间件解决方案,以解决服务集群/分布方面的问题,以实现负载平衡和可用性。我正在考虑基于消息传递系统(更具体地说,JMS)为此构建自己的基础设施。但是,如果可能的话,我宁愿使用已经存在的
在 Hadoop 集群中,数据是自动跨数据节点复制还是必须编程? 如果必须编程,那我该怎么做呢? 最佳答案 dfs.replication 的默认值为 3。这存在于您的 hdfs.site.xml 中
我已经设置了一个具有 1 个主节点和 2 个从节点的 redis 集群,哨兵在所有 3 个节点上运行。 在此设置之前,我的应用程序指向运行 Redis 实例的单个节点。 集群搭建完成后,我的应用应该指
所以,我正在设计一个具有多个 redis 实例的分布式系统来分解大量的流式写入,但发现很难清楚地了解事情是如何工作的。 从我读到的内容来看,正确配置的集群似乎会自动对“错误实例”上的请求进行分片和重定
我有一个关于redis集群架构的问题。 我正在设置一个 Redis 集群,并遵循基本建议:3 硕士3个奴隶 有什么方法可以在 Amazon LB、HAProxy、Nginx 等负载均衡器后面配置此集群
那么集群背后的想法是什么? 您有多台机器具有相同的数据库副本,您在其中传播读/写?这是正确的吗? 这个想法如何运作?当我进行选择查询时,集群会分析哪个服务器的读/写较少并将我的查询指向该服务器? 当您
目录 一.系统环境 二.前言 三.Kubernetes 3.1 概述 3.2 Kube
我是一名优秀的程序员,十分优秀!