当前位置: 首页 > wzjs >正文

可以做兼职的网站推荐杭州seo网站优化

可以做兼职的网站推荐,杭州seo网站优化,够完美网站建设,济南微网站建设目录 一、项目依赖二、配置 Redis三、配置 Spring Security四、配置 Redis 存储用户信息五、创建用户详情服务六、创建控制器七、创建登录页面八、总结 在现代 Web 开发中,使用 Redis 存储用户信息不仅可以提高系统的性能,还能有效降低数据库的访问压力。…

目录

    • 一、项目依赖
    • 二、配置 Redis
    • 三、配置 Spring Security
    • 四、配置 Redis 存储用户信息
    • 五、创建用户详情服务
    • 六、创建控制器
    • 七、创建登录页面
    • 八、总结

在现代 Web 开发中,使用 Redis 存储用户信息不仅可以提高系统的性能,还能有效降低数据库的访问压力。本文将介绍如何在 Spring Boot 应用中集成 Spring Security,并使用 Redis 存储用户信息,实现高效的用户认证和授权。

一、项目依赖

pom.xml 中添加以下依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId>
</dependency>

二、配置 Redis

application.properties 文件中添加 Redis 配置:

# Redis 配置
spring.redis.host=localhost
spring.redis.port=6379

三、配置 Spring Security

创建一个配置类 SecurityConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers("/", "/home", "/login").permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login").defaultSuccessUrl("/home", true).and().logout().logoutUrl("/logout").logoutSuccessUrl("/login?logout");}@Beanpublic BCryptPasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}
}

四、配置 Redis 存储用户信息

创建一个 RedisConfig.java 配置类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}
}

五、创建用户详情服务

创建一个 CustomUserDetailsService.java 类:

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.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;import java.util.Collections;
import java.util.concurrent.TimeUnit;@Service
public class CustomUserDetailsService implements UserDetailsService {private final RedisTemplate<String, Object> redisTemplate;private final PasswordEncoder passwordEncoder;@Autowiredpublic CustomUserDetailsService(RedisTemplate<String, Object> redisTemplate, PasswordEncoder passwordEncoder) {this.redisTemplate = redisTemplate;this.passwordEncoder = passwordEncoder;}@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {// 尝试从 Redis 中获取用户信息Object user = redisTemplate.opsForValue().get("user:" + username);if (user != null) {return new org.springframework.security.core.userdetails.User(username,passwordEncoder.encode("123456"),Collections.emptyList());}// 如果 Redis 中没有用户信息,可以从数据库加载(此处简化为直接返回)return new org.springframework.security.core.userdetails.User(username,passwordEncoder.encode("123456"),Collections.emptyList());}// 可以添加方法将用户信息存储到 Redispublic void saveUserToRedis(String username, Object user, long expirationInMinutes) {redisTemplate.opsForValue().set("user:" + username, user, expirationInMinutes, TimeUnit.MINUTES);}
}

六、创建控制器

创建一个 LoginController.java 类:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;@Controller
public class LoginController {@GetMapping("/login")public String login() {return "login";}@GetMapping("/home")public String home() {return "home";}
}

七、创建登录页面

src/main/resources/templates 目录下创建 login.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Login</title>
</head>
<body><h2>Login</h2><form action="/login" method="post"><div><label for="username">Username:</label><input type="text" id="username" name="username"></div><div><label for="password">Password:</label><input type="password" id="password" name="password"></div><div><button type="submit">Login</button></div></form>
</body>
</html>

八、总结

通过本文的介绍,你已经掌握了如何在 Spring Boot 应用中集成 Spring Security,并使用 Redis 存储用户信息。使用 Redis 存储用户信息可以提高系统的性能和响应速度,特别是在用户数量较多的情况下。希望本文的内容能够帮助你更好地应用 Spring Security 和 Redis,构建高效、安全的 Web 应用。

http://www.dtcms.com/wzjs/355926.html

相关文章:

  • java web医疗网站开发360站长
  • 做网站域名的成本辽宁好的百度seo公司
  • 网站 如何做用户统计百度账号中心
  • 专业网站设计团队网络营销推广渠道有哪些
  • 一级a做爰网站中国什么是网络营销与直播电商
  • 东莞招聘信息快手seo软件下载
  • b2b电子商务网站的模式网页模板免费下载
  • 企业网站建设报价友链交换网站源码
  • 无锡新区做网站公司网络推广公司官网
  • 网站建设费用如何做账沈阳专业seo排名优化公司
  • 怎么做彩票平台网站吗怎么做电商
  • 江苏做网站怎么收费多少关键词智能调词工具
  • 广州网站设计公司推荐哪家互联网广告公司排名前十
  • 网站权限怎么弄电话投放小网站
  • 网站的提示公告做滚动字幕爱站关键词挖掘软件
  • 设计企业网站步骤推广专家
  • 做网站好深圳百度关键
  • 做网站被用作非法用途百度开户联系方式
  • 小工作室做网站中国建设网官方网站
  • 大公司网站搭建公司交换友链
  • wordpress评论分页长沙优化排名推广
  • 佛山做网站seo深圳网络推广
  • 镇江网站建设优化排名东莞免费网站建设网络营销
  • 扬州建站公司高明搜索seo
  • wordpress 底部导航菜单网络优化的基本方法
  • 曲靖网站设计公司网络营销的整体概念
  • 在线客服网站源码手机优化软件哪个好用
  • 舟山高端网站建设b2b平台都有哪些网站
  • 淘金企业网站建设服务高端网站建设深圳
  • 深圳网站建设外贸公司排名上海最新新闻热点事件