spring nosql_使用Spring Security和NoSQL的Spring Boot

spring nosql

在前面的文章中,我们从一个SQL数据库提供用户和权威检索自定义查询设置弹簧安全配置。

如今,许多现代应用程序都使用NoSQL数据库。 Spring安全性不是NoSQL数据库的现成解决方案。

在这种情况下,我们需要通过实现自定义UserDetailsS​​ervice提供解决方案。

在此示例中,我们将使用MongoDB数据库。 我将使用docker映像,但是通过从官方网站下载来建立mongodb数据库是一样容易的。

这些是开始使用docker和mongodb的一些命令(如果不使用docker,请忽略它们)

#pull the mongo image
docker pull mongo
#create a mongo container
docker run --name some-mongo -d mongo
#get the docker container id
docker ps
#get the containers ip
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CID
#connection using the ip retrieved
mongo $mongodb_container_ip

然后,我们将编写一个简单的初始化脚本,称为createuser.js。 该脚本将创建一个包含用户信息的文档,例如用户名密码和授权。

use springsecurity
db.users.insert({"name":"John","surname":"doe","email":"john@doe.com","password":"cleartextpass","authorities":["user","admin"]})

我们将使用mongo cli执行它。

mongo 172.17.0.2:27017 < createuser.js

为了在mongodb中使用spring security,我们需要从users集合中检索用户信息。

第一步是将mongodb依赖项添加到我们的gradle文件中,包括mongodb驱动程序。 请注意,我们将使用名为“ customuserdetails”的配置文件。

group 'com.gkatzioura'
version '1.0-SNAPSHOT'buildscript {repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")}
}apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'sourceCompatibility = 1.8repositories {mavenCentral()
}dependencies {compile("org.springframework.boot:spring-boot-starter-web")compile("org.thymeleaf:thymeleaf-spring4")compile("org.springframework.boot:spring-boot-starter-security")compile("org.mongodb:mongo-java-driver:1.3")compile("org.slf4j:slf4j-api:1.6.6")compile("ch.qos.logback:logback-core:1.1.7")compile("ch.qos.logback:logback-classic:1.1.7")testCompile "junit:junit:4.11"
}bootRun {systemProperty "spring.profiles.active", "customuserdetails"
}

然后,我们将创建一个mongodb连接bean。

package com.gkatzioura.spring.security.config;import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;/*** Created by gkatzioura on 9/27/16.*/
@Configuration
@Profile("customuserdetails")
public class MongoConfiguration {@Beanpublic MongoClient createConnection() {//You should put your mongo ip herereturn new MongoClient("172.17.0.2:27017");}
}

然后,我们将创建一个自定义用户详细信息对象。

package com.gkatzioura.spring.security.model;import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;import java.util.Collection;
import java.util.List;/*** Created by gkatzioura on 9/27/16.*/
public class MongoUserDetails  implements UserDetails{private String username;private String password;private List<GrantedAuthority> grantedAuthorities;public MongoUserDetails(String username,String password,String[] authorities) {this.username = username;this.password = password;this.grantedAuthorities = AuthorityUtils.createAuthorityList(authorities);}@Overridepublic Collection<? extends GrantedAuthority> getAuthorities() {return grantedAuthorities;}@Overridepublic String getPassword() {return password;}@Overridepublic String getUsername() {return username;}@Overridepublic boolean isAccountNonExpired() {return true;}@Overridepublic boolean isAccountNonLocked() {return true;}@Overridepublic boolean isCredentialsNonExpired() {return true;}@Overridepublic boolean isEnabled() {return true;}
}

下一步,我们将添加一个自定义UserDetailsS​​ervice,以通过mongodb数据库检索用户详细信息。

package com.gkatzioura.spring.security.service;import com.gkatzioura.spring.security.model.MongoUserDetails;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
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;import java.util.List;/*** Created by gkatzioura on 9/27/16.*/
public class CustomerUserDetailsService implements UserDetailsService {@Autowiredprivate MongoClient mongoClient;@Overridepublic UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {MongoDatabase database = mongoClient.getDatabase("springsecurity");MongoCollection<Document> collection = database.getCollection("users");Document document = collection.find(Filters.eq("email",email)).first();if(document!=null) {String name = document.getString("name");String surname = document.getString("surname");String password = document.getString("password");List<String> authorities = (List<String>) document.get("authorities");MongoUserDetails mongoUserDetails = new MongoUserDetails(email,password,authorities.toArray(new String[authorities.size()]));return mongoUserDetails;}return null;}}

最后一步是使用我们先前实现的自定义UserDetailsS​​ervice提供spring安全配置。

package com.gkatzioura.spring.security.config;import com.gkatzioura.spring.security.service.CustomerUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;/*** Created by gkatzioura on 9/27/16.*/
@EnableWebSecurity
@Profile("customuserdetails")
public class CustomUserDetailsSecurityConfig extends WebSecurityConfigurerAdapter {@Beanpublic UserDetailsService mongoUserDetails() {return new CustomerUserDetailsService();}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {UserDetailsService userDetailsService = mongoUserDetails();auth.userDetailsService(userDetailsService);}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public").permitAll().anyRequest().authenticated().and().formLogin().permitAll().and().logout().permitAll();}}

运行应用程序问题

gradle bootRun

您可以在github上找到源代码

翻译自: https://www.javacodegeeks.com/2016/09/spring-boot-spring-security-nosql.html

spring nosql

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/335593.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ofbiz修改mysql_如何将OFBIZG的默认数据库更改mysql的方法(只求参考 )

ofbiz自带的数据库是Derby,这是一种小型的适合与测试系统的数据库,但不适合在产品级系统中使用,所以通常我们需要将ofbiz迁移到其它数据库上,下面我就以如何迁移至mysql为例,向大家讲述数据库迁移过程,迁移至其它数据库的过程类似.第一步:修改entityengine.xml文件.该文件的位置…

HH SaaS电商系统的供应商系统设计

供应商信息结构图 供应商类型 商城的供应商划分为专享型、共享型两种&#xff0c;但是租户和店铺供应商则都是“专享型”的。 共享型供应商发布的商品归属供应商自己的&#xff0c;商品档案供应商才有资格管理&#xff0c;所以spu_base需要保存供应商id&#xff0c;有供应商id…

c mysql 地址池_FreeRadius 根据mysql 下发指定地址池的地址...

一、使用radius本地文件存储IP地址。修改modules/ippoolippool main_pool {range-start 192.168.111.1range-stop 192.168.113.254netmask 255.255.255.0cache-size 800session-db ${db_dir}/db.ippoolip-index ${db_dir}/db.ipindexoverride nomaximum-timeout 0}在si…

aws faas_带有AWS Lambda和Java的无服务器FaaS

aws faas什么是无服务器架构&#xff1f; 无服务器架构在由第三方完全管理的临时容器中运行自定义代码。 自定义代码通常只是完整应用程序的一小部分。 也称为函数 。 这为无服务器架构提供了另一个名称&#xff0c;即功能即服务 &#xff08;FaaS&#xff09;。 该容器是短暂的…

跨境商品的进口税额显示

跨境商品的采购类型有三种&#xff1a;直邮、保税、一般贸易&#xff0c;而一般贸易的商品已经清关入境了&#xff0c;虽然是跨境商品&#xff0c;但是无需再清关&#xff0c;所以商品详情页无需显示进口税相关信息。 直邮跨境商品显示的进口税信息如下图所示&#xff1a; 保税…

HH SaaS电商系统的跨境商品展示、下单、清关、出库全流程设计

跨境商品的展示 后补 跨境商品的下单 在订单确认页面就要按SKU拆单&#xff0c;所以跨境销售订单的主单和子单是一对一的关系 多种进口渠道的商品在同个销售主单中&#xff0c;在进口清关、收货、货款结算时会出现问题&#xff0c;假设销售订单中有2种商品&#xff0c;一种…

coreldraw x8段落_CDR X8设置自定义文字为默认字体(二)

通过上一篇文章的介绍&#xff0c;我们已经了解到了在CorelDRAW中如何自定义设置默认字体&#xff0c;相关阅读可参阅&#xff1a;CDR X8设置文字为默认字体。其实在CorelDRAW软件中给用户提供方式不止是一种&#xff0c;本文将介绍更多关于设置默认字体的方法。1. 打开CorelDR…

javaone_代理的JavaOne 2016观察

javaone我无法参加JavaOne 2016&#xff0c;因此很高兴看到在线资源众多&#xff0c;使我能够基于JavaOne 2016内容进行观察。 我在本文中引用并简要描述了其中的一些JavaOne 2016资源&#xff0c;并根据这些资源的使用添加了一些我自己的观察结果。 正如Katharine在JavaOne综述…

组合商品和商品套餐(套装)的设计

文章目录商品套餐创建商品套餐活动的交互设计组合商品创建组合商品的交互设计商品套餐 商品套餐设计成一种促销活动&#xff0c;活动结束时间不设置表示“长期有效”商品套餐是指SKU和SKU的组合套餐&#xff0c;例如&#xff1a;iphone 6s 数据线 白色iphone 6s 充电器 白色&a…

antlr 4.7.1_新ANTLR 4.6的重要更改

antlr 4.7.1自上一个主要版本发布以来&#xff0c;已经过去了将近一年的时间&#xff0c;推出了新的ANTLR版本&#xff1a; 4.6 。 有很多新闻&#xff1a;新的目标&#xff0c;更好的性能&#xff0c;更好的错误处理以及ANTLR本身开发中的一些改进。 新目标 影响最大的新闻可…

电商系统下单时商品库存和销售状态如何处理

文章目录查看购物车时商品库存问题商品被下架了如何处理价格变动了如何处理促销活动到期了如何处理提交订单时库存问题商品被下架了如何处理价格变动了如何处理促销活动到期了如何处理使用的优惠券到期了如何处理选择商品时库存问题商品被下架了如何处理价格变动了如何处理促销…

json怎么读取数据库_如何:使用Json插入数据库并从中读取

json怎么读取数据库在本文中&#xff0c;我们将为Speedment创建一个插件&#xff0c;该插件使用Gson生成序列化和反序列化逻辑&#xff0c;从而使其在数据库实体和JSON字符串之间进行映射非常容易。 这将有助于展示Speedment代码生成的可扩展性&#xff0c;同时探索Gson库的一些…

django连接mysql步骤_使用Django连接Mysql数据库步骤

链接mysql步骤第一步&#xff1a;在终端下载pymysql文件–pip install pymysql第二步&#xff1a;在gjango项目的__init__文件中添加代码import pymysqlpymysql .install_as_MySQLdb()第三步&#xff1a;找到mysql的连接源&#xff0c;然后填信息&#xff0c;如果没有mysql驱动…

HH SaaS电商系统的商品发货策略设计

什么是发货策略 用来指定商品的发货仓库的策略方案&#xff0c;我们在实际的业务开展过程中&#xff0c;同种商品不同的销售渠道也许发货仓库不同&#xff0c;同种商品同个销售渠道在不同的业务开展阶段中也许发货仓库不同&#xff0c;所以需要通过灵活配置发货策略来满足此需…

mysql 上一篇_mysql取上一篇和下一篇的查询

$id 为当前文章 ID获取文章上一篇文章&#xff1a;SELECT id FROM table WHERE id>$id ORDER BY id ASC LIMIT 1获取文章下一篇文章&#xff1a;SELECT id FROM table WHERE id查询思路&#xff1a;获取与该文章同表相连的文章&#xff0c;如果根据分类获取相应的关联文章&a…

jhipster_JHipster入门,第3部分

jhipster欢迎回到本JHipster教程系列&#xff01; 在第一部分中&#xff0c;我们介绍了如何创建整体应用程序。 在第二部分中 &#xff0c;我们逐步创建了一个微服务应用程序&#xff08;这有点复杂&#xff09;。 对于那些正在努力使JHipster正常运转的人&#xff0c;我想着重…

什么是UID、UED、UXD、IXD、UCD、IAD,看这篇就足够了

文章目录UID&#xff1a; User Interface Design 用户界面设计UI&#xff1a;User Interface&#xff08;用户界面&#xff09;UID&#xff08;用户界面设计师&#xff09;UID认知现状UED&#xff1a; User Experience Design 用户体验设计UE or UX&#xff1a; User Experienc…

jhipster_JHipster入门,第2部分

jhipster所以你回来了&#xff01; 在本系列的最后一部分中 &#xff0c;我们采用了单片路线创建了一个JHipster应用程序。 这是红色药丸路线&#xff1b; 生活几乎与您习惯的一样。 但是也许您喜欢挑战。 也许您想超越红色药丸并尝试蓝色药丸。 在这种情况下&#xff0c;Blue…

HH SaaS电商系统的虚拟资金账户(钱包余额)设计

文章目录方案一&#xff0c;将资金账户抽象出来虚拟资金账户余额流水记录实体方案二&#xff0c;用户表直接保存资金余额余额流水记录实体方案一&#xff0c;将资金账户抽象出来 虚拟资金账户 P.S. 如果机构代码和消费代码有区分类型&#xff0c;那么资金账户表中就不必保存“…

jhipster_jHipster入门,第1部分

jhipster因此&#xff0c;您想保持技术的领先地位&#xff0c;但对所有活动部件感到不知所措。 你真幸运&#xff01; 这就是jHipster发光的地方。 如果您喜欢Ruby on Rails或Grails的方法来快速启动和运行应用程序&#xff0c;那么这可能是适合您的选择。 jHipster旨在使设置…