All Projects → spring-guides → Gs Securing Web

spring-guides / Gs Securing Web

Licence: apache-2.0
Securing a Web Application :: Learn how to protect your web application with Spring Security.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Gs Securing Web

Jwt Security Example
Spring Boot with Spring Security using JWT
Stars: ✭ 154 (-29.36%)
Mutual labels:  spring-security
Oauth2 Family Barrel
OAuth2全家桶项目。本项目演示了如何使用spring-boot、spring-security以及spring-security-oauth快速构建OAuth2服务框架体系。
Stars: ✭ 188 (-13.76%)
Mutual labels:  spring-security
Spring Microservice Sample
Spring Boot based Mircoservice sample
Stars: ✭ 199 (-8.72%)
Mutual labels:  spring-security
Pac4j
Security engine for Java (authentication, authorization, multi frameworks): OAuth, CAS, SAML, OpenID Connect, LDAP, JWT...
Stars: ✭ 2,097 (+861.93%)
Mutual labels:  spring-security
Mcloud
基于Spring Cloud,实现微服务中常用的基础模块,包括 OAuth2 认证服务,统一注册中心,系统监控中心, 统一配置中心,API网关以及熔断器
Stars: ✭ 185 (-15.14%)
Mutual labels:  spring-security
Castle Platform
Castle-Platform是一个以高性能、高扩展性为目标的java开发平台。它是spring-mvc, spring-data, spring-security, Querydsl, JPA, Redis, Mongodb, Neo4j, groovy-template, Thymeleaf, ExtJS6, dubbo, thrift的最佳实践。
Stars: ✭ 197 (-9.63%)
Mutual labels:  spring-security
Angular Spring Reactive Sample
RESTful API demos with Spring 5 WebFlux, Spring Boot 2, Spring Data Mongo, Spring Security, Spring Session and Angular 11
Stars: ✭ 153 (-29.82%)
Mutual labels:  spring-security
Spring Reddit Clone
Reddit clone built using Spring Boot, Spring Security with JPA Authentication, Spring Data JPA with MySQL, Spring MVC. The frontend is built using Angular - You can find the frontend source code here - https://github.com/SaiUpadhyayula/angular-reddit-clone
Stars: ✭ 210 (-3.67%)
Mutual labels:  spring-security
Awesome Spring
A curated list of awesome books, tutorials, courses, and resources for the Spring framework ecosystem.
Stars: ✭ 186 (-14.68%)
Mutual labels:  spring-security
Shoppingcart
E-Commerce Website Using Java - Spring MVC in Maven - PROJECT IS NOT MAINTAINED
Stars: ✭ 198 (-9.17%)
Mutual labels:  spring-security
Company Structure Spring Security Oauth2 Authorities
Example Spring Boot + Spring Security+ OAuth2 project for demonstration purposes.
Stars: ✭ 172 (-21.1%)
Mutual labels:  spring-security
Hospital
医院预约挂号微信小程序(Spring Boot、Vue、Uni-app)
Stars: ✭ 182 (-16.51%)
Mutual labels:  spring-security
Springcloud Shop
基于Spring Boot、Spring Cloud的微服务商城demo
Stars: ✭ 198 (-9.17%)
Mutual labels:  spring-security
Spring Samples
A series of examples used to demonstrate certain features of Spring.
Stars: ✭ 154 (-29.36%)
Mutual labels:  spring-security
Jwt Spring Security Jpa
Backend MVP showcasing JWT (Json Web Token) authentication with multiple login, timeout / refresh / logout (with in memory invalidation) using Spring Security & MySQL JPA.
Stars: ✭ 202 (-7.34%)
Mutual labels:  spring-security
Springcloud Oauth2
本项目基于spring-cloud-starter-oauth2搭建的认证中心和资源服务器的微服务项目,项目不仅仅简单的demo,项目的出发点在于实战应用。本项目为笔者花了不少时间和精力整理出来的,只需要稍微调整就可应用于实际项目当中,并且项目包含大量注释,不仅可以让你会用,也可让你了解到一些流程、一些原理上的东西。认证中心完成密码模式、授权码模式、刷新token模式、简化模式、以及自定义的手机号验证码模式。
Stars: ✭ 154 (-29.36%)
Mutual labels:  spring-security
Spring Webflux Security Jwt
A JWT authorization and authentication implementation with Spring Reactive Webflux, Spring Boot 2 and Spring Security 5
Stars: ✭ 190 (-12.84%)
Mutual labels:  spring-security
Tutorial
Spring Boot的例子,包含RESTful API, MVC, JMS, Cache, Mybatis, Cache, Websocket...
Stars: ✭ 215 (-1.38%)
Mutual labels:  spring-security
Registration Login Spring Hsql
Registration and Login Example with Spring Security, Spring Boot, Spring Data JPA, HSQL, JSP
Stars: ✭ 208 (-4.59%)
Mutual labels:  spring-security
Encrypt Body Spring Boot Starter
(停止维护,替代品搜索:https://github.com/search?l=Java&q=encrypt&type=Repositories )SpringBoot控制器统一的响应体加密与请求体解密的注解处理方式,支持MD5/SHA/AES/DES/RSA
Stars: ✭ 198 (-9.17%)
Mutual labels:  spring-security

:spring_version: current :spring_boot_version: 2.4.3 :DispatcherServlet: http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html :SpringApplication: http://docs.spring.io/spring-boot/docs/{spring_boot_version}/api/org/springframework/boot/SpringApplication.html :SpringBootSecurity: http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#boot-features-security :images: https://raw.githubusercontent.com/spring-guides/gs-securing-web/master/images :toc: :icons: font :source-highlighter: prettify :project_id: gs-securing-web

This guide walks you through the process of creating a simple web application with resources that are protected by Spring Security.

== What You Will Build

You will build a Spring MVC application that secures the page with a login form that is backed by a fixed list of users.

== What You Need

:java_version: 1.8 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]

[[scratch]] == Starting with Spring Initializr

If you use Maven, visit the https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=securing-web&name=securing-web&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.securing-web&dependencies=web,thymeleaf[Spring Initializr] to generate a new project with the required dependencies (Spring Web and Thymeleaf).

The following listing shows the pom.xml file that is created when you choose Maven:

==== [source,xml]

include::initial/pom.xml[]

====

If you use Gradle, visit the https://start.spring.io/#!type=gradle-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=securing-web&name=securing-web&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.securing-web&dependencies=web,thymeleaf[Spring Initializr] to generate a new project with the required dependencies (Spring Web and Thymeleaf).

The following listing shows the build.gradle file that is created when you choose Gradle:

==== [source,text]

include::initial/build.gradle[]

====

=== Manual Initialization (optional)

If you want to initialize the project manually rather than use the links shown earlier, follow the steps given below:

. Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you. . Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java. . Click Dependencies and select Spring Web and Thymeleaf. . Click Generate. . Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.

NOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE.

[[initial]] == Create an Unsecured Web Application

Before you can apply security to a web application, you need a web application to secure. This section walks you through creating a simple web application. Then you will secure it with Spring Security in the next section.

The web application includes two simple views: a home page and a "Hello, World" page. The home page is defined in the following Thymeleaf template (from src/main/resources/templates/home.html):

==== [source,html]

include::initial/src/main/resources/templates/home.html[]

====

This simple view includes a link to the /hello page, which is defined in the following Thymeleaf template (from src/main/resources/templates/hello.html):

==== [source,html]

include::initial/src/main/resources/templates/hello.html[]

====

The web application is based on Spring MVC. As a result, you need to configure Spring MVC and set up view controllers to expose these templates. The following listing (from src/main/java/com/example/securingweb/MvcConfig.java) shows a class that configures Spring MVC in the application:

==== [source,java]

include::initial/src/main/java/com/example/securingweb/MvcConfig.java[]

====

The addViewControllers() method (which overrides the method of the same name in WebMvcConfigurer) adds four view controllers. Two of the view controllers reference the view whose name is home (defined in home.html), and another references the view named hello (defined in hello.html). The fourth view controller references another view named login. You will create that view in the next section.

At this point, you could jump ahead to "<<run_the_app>>" and run the application without having to log in to anything.

Now that you have an unsecured web application, you can add security to it.

[[initial]] == Set up Spring Security

Suppose that you want to prevent unauthorized users from viewing the greeting page at /hello. As it is now, if visitors click the link on the home page, they see the greeting with no barriers to stop them. You need to add a barrier that forces the visitor to sign in before they can see that page.

You do that by configuring Spring Security in the application. If Spring Security is on the classpath, Spring Boot {SpringBootSecurity}[automatically secures all HTTP endpoints] with "basic" authentication. However, you can further customize the security settings. The first thing you need to do is add Spring Security to the classpath.

With Gradle, you need to add two lines (one for the application and one for testing) in the dependencies closure in build.gradle, as the following listing shows:

==== [source,java]

implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.security:spring-security-test'

====

The following listing shows the finished build.gradle file:

==== [source,text]

include::complete/build.gradle[]

====

With Maven, you need to add two extra entries (one for the application and one for testing) to the <dependencies> element in pom.xml, as the following listing shows:

==== [source,zml]

org.springframework.boot spring-boot-starter-security org.springframework.security spring-security-test test ---- ====

The following listing shows the finished pom.xml file:

==== [source,text]

include::complete/pom.xml[]

====

The following security configuration (from src/main/java/com/example/securingweb/WebSecurityConfig.java) ensures that only authenticated users can see the secret greeting:

==== [source,java]

include::complete/src/main/java/com/example/securingweb/WebSecurityConfig.java[]

====

The WebSecurityConfig class is annotated with @EnableWebSecurity to enable Spring Security's web security support and provide the Spring MVC integration. It also extends WebSecurityConfigurerAdapter and overrides a couple of its methods to set some specifics of the web security configuration.

The configure(HttpSecurity) method defines which URL paths should be secured and which should not. Specifically, the / and /home paths are configured to not require any authentication. All other paths must be authenticated.

When a user successfully logs in, they are redirected to the previously requested page that required authentication. There is a custom /login page (which is specified by loginPage()), and everyone is allowed to view it.

The userDetailsService() method sets up an in-memory user store with a single user. That user is given a user name of user, a password of password, and a role of USER.

Now you need to create the login page. There is already a view controller for the login view, so you need only to create the login view itself, as the following listing (from src/main/resources/templates/login.html) shows:

==== [source,html]

include::complete/src/main/resources/templates/login.html[]

====

This Thymeleaf template presents a form that captures a username and password and posts them to /login. As configured, Spring Security provides a filter that intercepts that request and authenticates the user. If the user fails to authenticate, the page is redirected to /login?error, and your page displays the appropriate error message. Upon successfully signing out, your application is sent to /login?logout, and your page displays the appropriate success message.

Last, you need to provide the visitor a way to display the current user name and sign out. To do so, update the hello.html to say hello to the current user and contain a Sign Out form, as the following listing (from src/main/resources/templates/hello.html) shows:

====

[source,html]

include::complete/src/main/resources/templates/hello.html[]

====

We display the username by using Spring Security's integration with HttpServletRequest#getRemoteUser(). The "Sign Out" form submits a POST to /logout. Upon successfully logging out, it redirects the user to /login?logout.

[[run_the_app]] == Run the Application

The Spring Initializr creates an application class for you. In this case, you need not modify the class. The following listing (from src/main/java/com/example/securingweb/SecuringWebApplication.java) shows the application class:

==== [source,java]

include::complete/src/main/java/com/example/securingweb/SecuringWebApplication.java[]

====

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_subhead.adoc[] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[]

Once the application starts up, point your browser to http://localhost:8080. You should see the home page, as the following image shows:

image::{images}/home.png[The application's home page]

When you click on the link, it attempts to take you to the greeting page at /hello. However, because that page is secured and you have not yet logged in, it takes you to the login page, as the following image shows:

image::{images}/login.png[The login page]

NOTE: If you jumped down here with the unsecured version, you do not see the login page. You should back up and write the rest of the security-based code.

At the login page, sign in as the test user by entering user and password for the username and password fields, respectively. Once you submit the login form, you are authenticated and then taken to the greeting page, as the following image shows:

image::{images}/greeting.png[The secured greeting page]

If you click on the Sign Out button, your authentication is revoked, and you are returned to the login page with a message indicating that you are logged out.

== Summary Congratulations! You have developed a simple web application that is secured with Spring Security.

== See Also

The following guides may also be helpful:

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].