Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize SpringSecurity Application

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to implement Spring Security application, the content is concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article hope you can gain something.

The first SpringSecurity app

Spring Security official introduction https://spring.io/projects/spring-security#learn

The goal of Spring Security is to address the question "Who are you? "What can you do? "Two questions.

Step 1. create a directory structure|____spring-security-learn-1||____build.gradle||____src|||____main||||____java||||____resources2. Create file build.gradle

Focus on introducing spring-boot-starter-security dependency. The complete gradle configuration is as follows:

plugins { id 'java' id "io.spring.dependency-management" version "1.0.8.RELEASE"}group 'net.txt100.learn'version '1.0'sourceCompatibility = 1.8apply plugin: 'application'mainClassName = 'net.txt100.learn.springsecurity.base.case1.Case1Application'repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public" } mavenCentral()}dependencyManagement { imports { mavenBom 'org.springframework.boot:spring-boot-dependencies:2.1.6.RELEASE' }}dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' // spring boot compile group: 'org.springframework.boot', name: 'spring-boot-starter-web' compile group: 'org.springframework.boot', name: 'spring-boot-starter-security'}3. Create a resource service UserController.javapackage net.txt100.learn.springsecurity.base.case1.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Title: UserController * Package: net.txt100.learn.springsecurity.base.case1.controller * Creation date: 2019-08-08 * Description: * * @author Tonglei * @since 1.0 */@RestController@RequestMapping("/user")public class UserController { @RequestMapping("/all") public String getAllUsers() { return "This is a protected resource/user/all"; }}4. Create spring-boot class Case1Application.javapackage net.txt100.learn.springsecurity.base.case1;import org.springframework.boot.SpringApplication;import org.springframework.boot. autofigure.SpringBootApplication;/** * @author Tonglei * @since 1.0 */@SpringBootApplicationpublic class Case1Application { public static void main(String[] args) { SpringApplication.run(Case1Application.class, args); }}5. compiled and executed

Open the command line, enter the project root directory, and execute the compilation command.

gradle compileJava

Execute Run Command

gradle run

Visit http://localhost:8080/user/all

The browser displays

Enter user name

Passwords are automatically generated each time you start and can be found in the logs

...

2019-08-08 15:13:10.028 INFO 824 --- [ main] .s.s.UserDetailsServiceAutoConfiguration :

Using generated security password: 8c20d4a7-7507-41ce-a271-a75fbe0c7dee

After authentication, you can see the content returned by the User controller

The simplest spring-security project simply adds spring-boot-starter-security to the project dependencies.

By default, all resource addresses for this project need to be authenticated successfully before they can be accessed. Default account user, password can be found in the log.

If you want to cancel the default security settings, you need to add the following to the configuration file:

security.basic.enabled = false #Spring-security security configuration disabled by default The above content is how to implement SpringSecurity applications. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report