springmvc--bcrypt 加密算法 +springsecurity-笔记

SpringSecurity与SpringMVC+MongoDB集成实践指南 SpringSecurity作为Java生态中主流的安全框架,通过过滤器链机制为Web应用提供认证授权功能。其核心原理是基于Servlet规范的安全过滤,与SpringMVC的DispatcherServlet协同工作。在NoSQL数据库场景下,与MongoDB的集成需要特殊处理用户存储逻辑。这种技术组合特别适合电商等需要灵活权限控制的系统,通过自定义UserDetailsService实现MongoDB用户查询,结合BCrypt加密保障密码安全。实践中需注意过滤器配置顺序和静态资源放行等常见问题。 阅读详情

BCrypt 加密出来的字符串 ,密码相同,加密出的密文字符串是不一样的!配合springSecurity使用

目录结构

pom.xml



        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>spring-demo</display-name>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <!-- 解决post乱码 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>Demo-7.html</welcome-file>
    </welcome-file-list>

<!--安全框架配置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-security.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--设置页面不登录也可以访问页面-->
    <http pattern="/*.html" security="none"></http>
    <http pattern="/*.ico" security="none"></http>
    <http pattern="/css/**" security="none"></http>
    <http pattern="/img/**" security="none"></http>
    <http pattern="/js/**" security="none"></http>
    <http pattern="/plugins/**" security="none"></http>
    <http pattern="/seller/add" security="none"></http>
    <!--页面的拦截规则 expressions 是否启用SPEL表达式-->
    <http use-expressions="false">
        <!--pattern :匹配 当前目录下的所有资源
            access: 角色名称 当前用户必须有ROLE_USER的角色才能访问资源-->
        <intercept-url pattern="/**" access="ROLE_SELLER"/>
        <!--开启表单登录功能
        always-use-default-target= 总是跳转默认页面
        login-page登录页面
        default-target-url 登录成功页面
        authentication-failure-url 登录失败页面
        login-processing-url 请求地址配置
        -->
        <form-login always-use-default-target="false"
                    login-page="/shoplogin.html"
                    default-target-url="/admin/index.html"
                    authentication-failure-url="/shoplogin.html"
                    />
        <csrf disabled="true"/>
        <!--可以使用内置框架也-->
        <headers>
            <frame-options policy="SAMEORIGIN"/>
        </headers>
        <!--自动产生退出地址
        logout-success-url 指定退出的地址-->
        <logout />
        <!--<logout logout-success-url="/login.html"/>-->
    </http>


    <!--认证管理器-->
    <authentication-manager>
        <!--认证提供者-->
        <authentication-provider user-service-ref="userDetailsService">
            <password-encoder ref="passwordEncoder"></password-encoder>
            <!--<user-service>-->
                <!--&lt;!&ndash;定义用户&ndash;&gt;-->
                <!--<user name="admin"  password="123456" authorities="ROLE_ADMIN"/>-->
                <!--<user name="sunwukong"  password="dasheng" authorities="ROLE_ADMIN"/>-->
            <!--</user-service>-->
        </authentication-provider>
    </authentication-manager>


    <beans:bean id="userDetailsService" class="cn.bufanli.service.UserDetailsServiceImpl">
        <beans:property name="sellerService" ref="sellerService"></beans:property>
    </beans:bean>

    <!-- 引用dubbo 服务 -->
    <dubbo:application name="pinyougou-shop-web" />
    <dubbo:registry address="zookeeper://IP:PORT"/>
    <dubbo:reference id="sellerService" interface="cn.bufanli.sellergoods.service.SellerService"></dubbo:reference>
    <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></beans:bean>
</beans:beans>

加密代码

package cn.bufanli.shop.controller;

import cn.bufanli.pojo.TbSeller;
import cn.bufanli.sellergoods.service.SellerService;
import cn.bufanli.utiles.Message;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * controller
 *`
 * @author Administrator
 */
@RestController
@RequestMapping("/seller")
public class SellerController {
     @Reference
     private SellerService sellerService;

     /**
      * 增加
      *
      * @param seller
      * @return
      */
     @RequestMapping("/add")
     public Message add(@RequestBody TbSeller seller) {
          //将密码进行加密
          BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
          String pas = passwordEncoder.encode(seller.getPassword());
          seller.setPassword(pas);
          Message add = sellerService.add(seller);
          System.out.println(add);
          return add;

     }
}

认证类

package cn.bufanli.service;

import cn.bufanli.pojo.TbSeller;
import cn.bufanli.sellergoods.service.SellerService;
import cn.bufanli.utiles.Message;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 认证类
 *
 * @author BuShuangLi
 * @date 2019/3/5
 */
public class UserDetailsServiceImpl implements UserDetailsService {

     private SellerService sellerService;

     public void setSellerService(SellerService sellerService) {
          this.sellerService = sellerService;
     }

     /**
      * 用户在页面输入的用户名
      * @param username
      * @return
      * @throws UsernameNotFoundException
      */
     @Override
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          System.out.println("---经过了UserDetailsService----");
          //构建角色列表
          List<GrantedAuthority> grantAuths =new ArrayList();
          grantAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));
          //获取用户对象
          Message one = sellerService.findOne(username);

          //查到对应的对象
          if("200".equals(one.getStatus())){
               TbSeller data =  (TbSeller)one.getData();
               if ("1".equals(data.getStatus())){
                    return new User(username,data.getPassword(),grantAuths);
               }else{
                    return null;
               }
          }else{
               return null;

          }

     }
}

页面from表单  action 必须="/login" method必须="post"

<form class="sui-form" action="/login" method="post" id="loginFrom">
    <div class="input-prepend"><span class="add-on loginname"></span>
        <input id="prependedInput" name="username" type="text" placeholder="邮箱/用户名/手                    机号" class="span2 input-xfat">
    </div>
    <div class="input-prepend"><span class="add-on loginpwd"></span>
        <input id="prependedInput" type="password" name="password" placeholder="请输入密码" class="span2 input-xfat">
    </div>
    <div class="setting">
        <label class="checkbox inline"><input name="m1" type="checkbox" value="2" checked="">自动登录</label>
    <span class="forget">忘记密码?</span>
    </div>
    <div class="logined">
        <a class="sui-btn btn-block btn-xlarge btn-danger" onclick="document:   loginFrom.submit()" target="_blank">登&nbsp;&nbsp;录</a>
    </div>
</form>

 

【AI编程】Qoder AI 编程工具从部署到深度使用实战详解 Qoder AI 编程工具从部署到深度使用实战详解 阅读详情

相关推荐

01 AUTOSAR CP标准文档阅读指南

01 AUTOSAR CP标准文档阅读指南

汽车软件工程师 | 好奇博主 | 处女座 1142

BCryptPasswordEncoder的使用及原理

当时看到使用BCryptPasswordEncoder时,同样的密码可以生成不同的密文而且还可以通过matches方法进行匹配验证,觉得很神奇。后来经过调试发现,密文中本身包含了很多信息,包括salt和使用salt加密后的hash。因为每次的salt不同,因此每次的hash也不同。这样就可以使得相同的明文生成不同的密文,而密文中包含salt和hash,因此验证过程和生成过程也是相同的。附一张大致的调用关系流程图,供大家参考。https。...

m0_67391121的博客 1141

达梦数据库连接报错6001网络通信异常

本文主要记录客户端连接达梦数据库报错6001网络通信异常问题分析过程、问题原因以及问题解决方法。

chen_zizi2025的博客 2784

SpringMVC项目——数据库账户密码加密

目录 1:为什么需要账户密码加密 2:没有加密之前 3:加密实现 3.1:加密算法代码实现 3.2:配置文件更改 1:为什么需要账户密码加密 在web项目中我们常常把数据库信息写入到配置文件中,便于降低代码耦合,但是明文的密码安全性较低。比如在同一个公司里边,有具体的运维实施团队,人多嘴杂。让他们部署不同的项目组的文件时,有经验的实施人员可能会查看你的配置文件中的数据账户密码信息,如...

huyiju的博客 6091

【机械臂算法】基于最小惯性参数集的动力学辨识仿真实践

本文深入探讨了机械臂动力学参数辨识的核心挑战与解决方案,重点阐述了基于最小惯性参数集的理论与方法。通过分析PUMA560实例,详细介绍了利用QR分解法或符号推导法获取可辨识参数集,并系统讲解了从激励轨迹设计、仿真数据采集、线性系统求解到模型验证的完整实践流程,为高精度动力学建模与控制奠定了坚实基础。

weixin_29054399的博客 381

保姆级教程:在Ubuntu 20.04上从零搞定ROS Noetic和RealSense驱动,为无人机开发铺路

本文提供了一份详细的Ubuntu 20.04环境下ROS Noetic和RealSense驱动安装指南,专为无人机开发环境搭建设计。从系统配置、ROS安装到RealSense驱动集成,涵盖所有关键步骤和常见问题解决方案,帮助开发者快速搭建自主空中机器人开发平台。

weixin_30329623的博客 65

SpringSecurity OAuth2.0-第3章: spring mvc集成spring security

一 spring security spring security是一个封装比较完整安全的认证授权框架。是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,在spring boot项目中加入spring security更是十分简单,使用Spring Security 减少了为企业系统安全控制编写大量重复代码的工作。 ...

健康平安的活着的专栏 4170

RK3588交叉编译实战:从环境搭建到程序部署

本文详细介绍了在X86主机上为RK3588开发板搭建交叉编译环境的完整流程。从获取ARM工具链、配置sysroot,到实战编译Hello World和Qt应用程序,并提供了部署方法和常见问题解决方案。通过交叉编译,开发者可以在高性能PC上高效开发,并在ARM架构的RK3588上运行程序,大幅提升嵌入式开发效率。

weixin_29191669的博客 279

spring mvc和springboot 配置文件加密

1、spring mvc 配置文件加密,直接上代码: 流程:编写EncryptPropertyPlaceholderConfigurer --> 配置spring-context.xml public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { /** * 需要加解密的配置文件key */ private String[] ...

qq355972697的专栏 8528

《Zephyr OS 入门与实战》第5章:内存管理策略

【代码】《Zephyr OS 入门与实战》第5章:内存管理策略。

2301_79143213的博客 353

基于RK3568的YOLOv11模型端侧推理优化与部署实战指南

在AI边缘计算领域,瑞芯微RK3568芯片凭借其强大的NPU算力和丰富的接口资源,成为了众多开发者的首选平台。然而,瑞芯微官方文档虽然提供了YOLO11预训练模型的部署方案,但对于需要自定义数据集训练的开发者来说,从模型训练到板端部署的完整流程却缺乏详细指导。本文将带你走完从环境搭建、数据准备、模型训练到最终板端部署的完整技术路线,并分享实际开发中遇到的各种坑点和解决方案。让我们一起探索如何在RK3568平台上实现30FPS稳定推理的YOLO11目标检测应用!

m0_64661770的博客 116

三种springMVC项目、springboot项目配置文件加密解密方法

三种springMVC项目、springboot项目配置文件加密解密方法一、springMVC模式二、springBoot项目 为了项目的安全,今天项目需要用到配置文件(有XML和Java两种文件的)加解密,由于项目年久失修,项目逐渐分出了多个服务,有springMVC模式的,也有springBoot版本的,今天给大家呈现这两种项目的自定义加密方式。 一、springMVC模式 1.自定义一个类,...

TK305的博客 3148

GEE生物量预测:利用多源遥感数据和随机森林算法RF构建地上生物量(AGB)预测模型,并实现未来年份的生态变化模拟(大面积生物量反演)

数据融合策略:整合5类遥感产品(AGB、NDVI、LAI、土地覆盖、地形)时序特征构建:采用生长季中值法降低云污染影响机器学习优化分层采样保证数据代表性80棵决策树平衡精度与效率预测可靠性基准期R²达0.76需结合地面实测数据校正。

此星光明博客 421

RSA加密算法及其与SpringMVC集成

如有不足,敬请各位提出批评,定会改正。THX!   本文介绍的是RSA加密算法+Spring Security在SpringMVC中的集成使用。 Spring Security是什么?  引用:   Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的B

u014386474的博客 1521

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

知识点系列 SpringSecurity 基础

如果我们想指定系统的访问用户名及密码, 可以通过配置的形式声明 , 声明一个 UserDetailsService 类型的Bean。@EnableWebSecurity//开启web安全设置生效/*** 构建认证服务,并将对象注入spring IOC容器,用户登录时,会调用该服务进行用户合法信息认证* @return*/@Bean//构建用户。

weixin_62868720的博客 766

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

SpringSecurity与权限

1.背景介绍 1. 背景介绍 Spring Security 是 Spring 生态系统中的一个重要组件,用于实现应用程序的安全性和访问控制。它提供了一种简单且可扩展的方法来保护应用程序的数据和资源,确保它们只能被授权的用户访问。Spring Security 支持多种身份验证和授权机制,包括基于用户名和密码的身份验证、基于 OAuth 的授权、基于角色和权限的访问控制等。 在现代应用程序中...

AI天才研究院 480

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

springboot学习笔记-多人互动blog系统

SpringMVC @GetMapping注释将 HTTP GET 请求映射到特定的处理程序方法。 它是一个组合的注释,用作@RequestMapping(method = RequestMethod.GET)的快捷方式。@GetMapping将从 GET 请求到index()方法的/根路径映射。 它返回纯文本 @PostMapping注释将 HTTP POST 请求映射到特定的处理程序方法。 它是一个组合的注释,用作@RequestMapping(method = RequestMethod.POST

qq_49173222的博客 523

企业WEB项目安全框架与商家入驻审核

一、Spring Security框架 1.Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码

JYplute的博客 596

springsecurity OAuth2.0-第4章: springboot整合 spring security认证与授权

一 spring security DaoAuthenticationProvider和UserDetailsService的职责搞混淆,其实UserDetailsService只负责从特定的地方(通常是数据库)加载用户信息,仅此而已。而DaoAuthenticationProvider的职责更大,它完成完整的认证流程,同时会把UserDetails填充至Authentication。 二 spring boot整合spring security 2.1 工程结构 2.2配置pom文件

健康平安的活着的专栏 857

Springboot数据加密篇

哈希算法,又称摘要算法(Digest),是一种将任意长度的输入通过散列函数变换成固定长度的输出的单向密码体制。这种映射的规则就是哈希算法,而通过原始数据映射之后得到的二进制值串就是哈希值。哈希算法最重要的特点就是:相同的输入一定得到相同的输出,不同的输入可能得到相同的输出,但不可能通过输出反推出原始输入。这意味着哈希算法可以用于快速比较两个数据是否相同,常用于密码存储、数字签名、数据完整性校验等领域。

qq_62377885的博客 6977

Spring安全教程

Spring Security是一个灵活而强大的身份验证和访问控制框架,用于保护基于Spring的Java Web应用程序。 本教程中使用的Spring版本: 春天3.2.8。发布 Spring Security 3.2.3发布 注意 试试这个Spring Boot + Spring Security + Thymeleaf示例 1. Spring安全示例 ...

一名可爱的技术搬运工 269

【web++_第四阶_中间学习阶段_学习security始于这两篇_4】

【看不太明白的】 http://www.cnblogs.com/yjmyzz/p/3856015.html   Spring Security笔记:使用BCrypt算法加密存储登录密码 http://www.cnblogs.com/fyx158497308/p/3986056.html   【仅一点:阻止用户重复登陆, remember me ,记住我;http 协议控制;访问端口控制;】

DNMCM的专栏 552

浙江理工大学信息学院本科生创新项目总结报告——简单的后台用户管理项目

信息学院本科生创新项目总结报告 研究目的与意义 基于Spring Boot框架等技术实现一个简单的后台用户管理系统项目,具有良好的用户交互体验、良好的信息反馈能力、安全防护措施,包括用户登录、用户注册、信息反馈、用户信息查询、管理员用户管理、权限控制与管理等功能。 通过对Spring Boot、Spring MVC、MyBatis、Spring Security等框架和相关技术的应用,...

无限迭代中...... 1555

@EnableWebSecurity 注解的用途及适用场景

注解是一个重要的功能,用于启用 Spring Security 的 Web 安全功能。注解适用于需要实现 Web 安全功能的应用程序,可以帮助开发者快速构建一个安全、可靠的应用程序。Spring Security 提供了丰富的安全配置选项,可以定制登录页面、登录成功/失败处理、权限验证等。Spring Security 支持基于角色的访问控制,可以根据用户的角色来决定是否允许访问特定的资源。Spring Security 提供了会话管理功能,可以控制会话的创建、销毁、超时等。

m0_67544708的博客 649

11_PROJECT_DAY04

8. 在SpringMVC框架中统一处理异常 在SpringMVC框架中提供了统一处理异常的机制(当然,在SpringBoot框架中也可以直接使用),使得每种异常只需要被处理1次即可,即使某种异常在多种请求中都会出现,也不需要反复处理!其核心是开发人员调用了可能抛出异常的方法时,在控制器中,直接将异常再次抛出,则SpringMVC在调用控制器的方法时,就会捕获到对应的异常对象,并且,如果开发人员定义了统一处理异常的方法,则SpringMVC框架就会自动调用该方法来处理异常! 关于统一处理异常的方法: 默

来自50年后的地球????博客 241

Spring MVC4 + Spring Security4 + Hibernate实例

http://www.yiibai.com/spring-security/spring-mvc-4-and-spring-security-4-integration-example.html 在这篇教程文章中,我们将使用Spring Security,Hibernate+MySQL数据库来集成构建一个成熟的Spring MVC应用程序。处理多对多映射关系,同时利用BCrypt格式加密密码存储,...

weixin_34138056的博客 321
上一篇: html--监控div内文本内容修改
下一篇: springmvc--sso单点登录cas统一身份认证器
别浪呀
博客等级 码龄8年 1万+粉丝 260原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值