07
2020
05

如何在Spring Boot应用程序中实现Spring安全性?

实施需要最少的配置。您需要做的就是spring-boot-starter-security在pom.xml文件中添加starter。您还需要创建一个Spring配置类,它将覆盖所需的方法,同时扩展 WebSecurityConfigurerAdapter 应用程序中的安全性。这是一些示例代码:


<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; word-break: initial; overflow-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.gkatzioura.security.securityendpoints.config;

import org.springframework.context.annotation.Configuration;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()

.antMatchers("/welcome").permitAll()

.anyRequest().authenticated()

.and()

.formLogin()

.permitAll()

.and()

.logout()

.permitAll();

}

}</pre>

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。