当前位置: 首页 > 软件库 > Web应用开发 > >

jwt-spring-security-demo

授权协议 MIT License
开发语言 Java
所属分类 Web应用开发
软件类型 开源软件
地区 不详
投 递 者 阎烨
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

JWT Spring Security Demo

About

This is a demo for using JWT (JSON Web Token) with Spring Security andSpring Boot. I completely rewrote my first version. Now this solution is based on the code basefrom the JHipster Project. I tried to extract the minimal configuration and classes that are neededfor JWT-Authentication and did some changes.

Requirements

This demo is build with with Maven 3.6.x and Java 11.

Usage

Just start the application with the Spring Boot maven plugin (mvn spring-boot:run). The application isrunning at http://localhost:8080.

You can use the H2-Console for exploring the database under http://localhost:8080/h2-console:

Backend

There are three user accounts present to demonstrate the different levels of access to the endpoints inthe API and the different authorization exceptions:

Admin - admin:admin
User - user:password
Disabled - disabled:password (this user is deactivated)

There are four endpoints that are reasonable for the demo:

/api/authenticate - authentication endpoint with unrestricted access
/api/user - returns detail information for an authenticated user (a valid JWT token must be present in the request header)
/api/persons - an example endpoint that is restricted to authorized users with the authority 'ROLE_USER' (a valid JWT token must be present in the request header)
/api/hiddenmessage - an example endpoint that is restricted to authorized users with the authority 'ROLE_ADMIN' (a valid JWT token must be present in the request header)

Frontend

I've written a small Javascript client and put some comments in the code that hopefully makes this demo understandable.You can find it at /src/main/resources/static/js/client.js.

Generating password hashes for new users

I'm using bcrypt to encode passwords. Your can generate your hashes with this simpletool: Bcrypt Generator

Using another database

Actually this demo is using an embedded H2 database that is automatically configured by Spring Boot. If you want to connectto another database you have to specify the connection in the application.yml in the resource directory. Here is an example for a MySQL DB:

spring:
  jpa:
    hibernate:
      # possible values: validate | update | create | create-drop
      ddl-auto: create-drop
  datasource:
    url: jdbc:mysql://localhost/myDatabase
    username: myUser
    password: myPassword
    driver-class-name: com.mysql.jdbc.Driver

Hint: For other databases like MySQL sequences don't work for ID generation. So you have to change the GenerationType in the entity beans to 'AUTO' or 'IDENTITY'.

You can find a reference of all application properties here.

Using Flyway

https://github.com/szerhusenBC/jwt-spring-security-demo/issues/81

Docker

This project has a docker image. You can find it at https://hub.docker.com/r/hubae/jwt-spring-security-demo/.

Questions

If you have project related questions please take a look at the past questions or create a new ticket with your question.

If you have questions that are not directly related to this project (e.g. common questions to the Spring Framework or Spring Security etc.) please search the web or look at Stackoverflow.

Sorry for that but I'm very busy right now and don't have much time.

Interesting projects

Author

Stephan Zerhusen

Copyright and license

The code is released under the MIT license.


Please feel free to send me some feedback or questions!

  • SpringDemo 是 Spring boot + Spring Security + JWT 整合的项目。 参考 securing-spring-boot-with-jwts. Spring Security 框架 依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-sta

  • 一,前言 我发现很多开源项目在权限安全这方面用的都是Spring-Security。所以这段时间学习了 Spring-Security ,做了一个权限管理demo巩固一下。之前有做过一个 shiro 的权限管理demo,前端就是借用那个项目的,只有一点地方改变。shiro 二,代码实践 1,POM 不止有这几个依赖,后面会提供源码 <dependency>

  • 在我的用户密码授权文章里介绍了spring-security的工作过程,在 用户密码授权模式里,主要是通过一个登陆页进行授权,然后把授权对象写到session里,它主要用在mvc框架里,而对于webapi来说,一般不会采用这种方式,对于webapi 来说,一般会用jwt授权方式,就是token授权码的方式,每访问api接口时,在http头上带着你的token码,而大叔自己也写了一个简单的jwt授权

 相关资料
  • 这是我启动应用程序时的控制台: 但是当我用邮递员访问时,我有一个错误:

  • 我想使用Spring Security设置一个中央身份验证/授权服务器,从那里我可以获取JWT令牌,然后我可以使用该令牌访问另一个Spring Security备份的REST服务器上的受限资源。 以下是我的流程: 我认为JWT最适合这个场景,因为它可以包含所有相关的数据,并且REST服务器可以是完全无状态的,只需解码令牌就可以获得所有必要的数据(role、clientid、email...)在RE

  • 我有一个复杂的情况,我需要在tomcat 8上实现一个同时提供静态html和rest服务的Web应用程序的安全性。该应用程序是Spring mvc应用程序(没有Spring启动) 身份验证(sso)过程如下所示: 如果http头中不存在用户jwt,则使用ldap进行身份验证,从db获取用户权限,并将jwt创建回用户。 如果标头中存在jwt,则跳过ldap过滤,从令牌中提取用户权限。 我想到了第一个

  • 我正在开发一个rest服务,它将通过浏览器、单页应用程序和移动应用程序在浏览器中提供。现在我的服务没有Spring就可以工作了。oauth2客户机是在过滤器内部实现的,可以说是“手工”。 我正在尝试将其迁移到spring Boot。阅读了大量的手册和谷歌搜索了大量的信息,我正在努力了解以下内容是否对客户来说是可能的: > 通过Spring-Security-Oauth2提供的所有帮助,使用face

  • 我对Spring保安很陌生。我试图在我的应用程序中实现来执行。我学习了示例代码,并且能够生成。但是根据示例文件中的配置,我可以使用配置中的内存数据集来验证中的输入。但是,我不理解如何将中发送的和与我的中的值进行检查。请引导我。我的配置类是 在重写的中,我可以设置将与路径的中存在的凭据进行比较的凭据

  • 我必须说,我对整个模型非常困惑,我需要帮助把所有的浮动件粘在一起。 我不是在做Spring REST,只是简单的WebMVC控制器。 什么让人困惑?(错误之处请指正) 第三方身份验证 要针对第三方进行身份验证,我需要通过扩展AuthenticationProvider来拥有自定义提供程序 null 问题: 何时调用AbstractAuthenticationProcessingFilter#Suc