当前位置: 首页 > news >正文

微信导航网站怎么做网络推广员

微信导航网站怎么做,网络推广员,珠海市企业网站建设,wordpress 仿煎蛋妹子图Spring Boot中的安全配置与实现 大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们将深入探讨Spring Boot中的安全配置与实现,看看如何保护你的…

Spring Boot中的安全配置与实现

大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们将深入探讨Spring Boot中的安全配置与实现,看看如何保护你的应用免受潜在的安全威胁。

一、Spring Boot中的安全框架简介

Spring Boot集成了Spring Security,这是一个强大的认证和授权框架,用于保护基于Spring的应用程序。Spring Security提供了许多功能,如基于角色的访问控制、表单登录、HTTP Basic认证、OAuth 2.0支持等。

1. Maven依赖

首先,确保在pom.xml文件中添加Spring Security的依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>

二、基本的安全配置

1. 创建安全配置类

创建一个继承自WebSecurityConfigurerAdapter的配置类,用于定义安全策略。

package cn.juwatech.springboot.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("user").password(passwordEncoder().encode("password")).roles("USER").and().withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN").antMatchers("/user/**").hasRole("USER").anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll();}@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}
}

2. 配置登录页面

创建一个简单的登录页面login.html,放置在src/main/resources/templates目录下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>Login</title>
</head>
<body><div><h2>Login</h2><form th:action="@{/login}" method="post"><div><label>Username: <input type="text" name="username"></label></div><div><label>Password: <input type="password" name="password"></label></div><div><input type="submit" value="Sign in"></div></form></div>
</body>
</html>

三、基于注解的安全控制

Spring Security支持基于注解的安全控制,使用@PreAuthorize@Secured注解可以在方法级别进行权限控制。

1. 使用@Secured注解

在服务类的方法上使用@Secured注解,指定角色权限。

package cn.juwatech.springboot.service;import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;@Service
public class UserService {@Secured("ROLE_ADMIN")public String adminMethod() {return "Admin access only";}@Secured("ROLE_USER")public String userMethod() {return "User access only";}
}

2. 使用@PreAuthorize注解

使用@PreAuthorize注解支持SpEL(Spring Expression Language)表达式,实现更复杂的权限控制。

package cn.juwatech.springboot.service;import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;@Service
public class SecureService {@PreAuthorize("hasRole('ADMIN')")public String adminOnly() {return "Admin access only";}@PreAuthorize("hasRole('USER') and #id == principal.id")public String userOnly(Long id) {return "User access for ID: " + id;}
}

四、使用JWT进行安全认证

JWT(JSON Web Token)是一种轻量级的认证机制,常用于移动和Web应用的认证。

1. 添加JWT依赖

pom.xml中添加JWT相关依赖:

<dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.9.1</version>
</dependency>

2. 创建JWT工具类

实现一个JWT工具类,负责生成和解析JWT。

package cn.juwatech.springboot.security;import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;import java.util.Date;@Component
public class JwtUtil {private String secretKey = "secret";public String generateToken(String username) {return Jwts.builder().setSubject(username).setIssuedAt(new Date()).setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10)).signWith(SignatureAlgorithm.HS256, secretKey).compact();}public Claims extractClaims(String token) {return Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody();}public String extractUsername(String token) {return extractClaims(token).getSubject();}public boolean isTokenExpired(String token) {return extractClaims(token).getExpiration().before(new Date());}public boolean validateToken(String token, String username) {return (username.equals(extractUsername(token)) && !isTokenExpired(token));}
}

3. 集成JWT认证

在Spring Security配置中集成JWT认证。

package cn.juwatech.springboot.config;import cn.juwatech.springboot.security.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate JwtUtil jwtUtil;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("user").password(passwordEncoder().encode("password")).roles("USER").and().withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers("/login").permitAll().antMatchers("/admin/**").hasRole("ADMIN").antMatchers("/user/**").hasRole("USER").anyRequest().authenticated().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);http.addFilterBefore(new JwtRequestFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class);}@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}
}

四、总结

通过本文,我们全面了解了在Spring Boot中实现安全配置的各种方法,包括基本的安全配置、基于注解的权限控制以及如何集成JWT进行认证。Spring Security提供了丰富的功能,使得应用程序的安全性得到有效保障。

微赚淘客系统3.0小编出品,必属精品!


文章转载自:
http://dinncoultrafiltrate.bpmz.cn
http://dinncoaviatic.bpmz.cn
http://dinncopterygotus.bpmz.cn
http://dinncobaguio.bpmz.cn
http://dinncomuscovite.bpmz.cn
http://dinncofittingly.bpmz.cn
http://dinncosarcenet.bpmz.cn
http://dinncoslogan.bpmz.cn
http://dinncoectopia.bpmz.cn
http://dinncopapalize.bpmz.cn
http://dinncoverbalizable.bpmz.cn
http://dinncobandgap.bpmz.cn
http://dinncoupwind.bpmz.cn
http://dinncounpremeditated.bpmz.cn
http://dinncosadu.bpmz.cn
http://dinncochoucroute.bpmz.cn
http://dinncosciential.bpmz.cn
http://dinncomindel.bpmz.cn
http://dinncoconfigurable.bpmz.cn
http://dinncopileup.bpmz.cn
http://dinncoswan.bpmz.cn
http://dinncofootwell.bpmz.cn
http://dinncoinsuppressible.bpmz.cn
http://dinncoendomorph.bpmz.cn
http://dinncoillinium.bpmz.cn
http://dinncobackout.bpmz.cn
http://dinncooxydation.bpmz.cn
http://dinncolaughton.bpmz.cn
http://dinncobuganda.bpmz.cn
http://dinncocero.bpmz.cn
http://dinncospringer.bpmz.cn
http://dinncoorogeny.bpmz.cn
http://dinncoonshore.bpmz.cn
http://dinncocanuck.bpmz.cn
http://dinncoatrabiliar.bpmz.cn
http://dinncoegyptologist.bpmz.cn
http://dinncobronchial.bpmz.cn
http://dinncofumigate.bpmz.cn
http://dinncoviscousness.bpmz.cn
http://dinncosheriffalty.bpmz.cn
http://dinncorecurvate.bpmz.cn
http://dinncoodds.bpmz.cn
http://dinncoschmitt.bpmz.cn
http://dinncokebele.bpmz.cn
http://dinncosemiurban.bpmz.cn
http://dinncometathoracic.bpmz.cn
http://dinncodraftsmanship.bpmz.cn
http://dinncodicky.bpmz.cn
http://dinncogreenstone.bpmz.cn
http://dinncosubstitutional.bpmz.cn
http://dinncorowel.bpmz.cn
http://dinncobeardtongue.bpmz.cn
http://dinncoreimburse.bpmz.cn
http://dinncoserang.bpmz.cn
http://dinncosubmersible.bpmz.cn
http://dinncoperistyle.bpmz.cn
http://dinncoadenology.bpmz.cn
http://dinncovassalage.bpmz.cn
http://dinncohopping.bpmz.cn
http://dinncounspoken.bpmz.cn
http://dinncoredesign.bpmz.cn
http://dinncopostposition.bpmz.cn
http://dinnconewsboy.bpmz.cn
http://dinncononsignificant.bpmz.cn
http://dinncofatalize.bpmz.cn
http://dinncomultiband.bpmz.cn
http://dinncorevolve.bpmz.cn
http://dinncoepaxial.bpmz.cn
http://dinncopurchasable.bpmz.cn
http://dinncoaethelbert.bpmz.cn
http://dinncoinnocency.bpmz.cn
http://dinncoratability.bpmz.cn
http://dinncoems.bpmz.cn
http://dinncogatorade.bpmz.cn
http://dinncoalfilaria.bpmz.cn
http://dinncohyalographer.bpmz.cn
http://dinncopiedmont.bpmz.cn
http://dinncolegitimist.bpmz.cn
http://dinncobumpity.bpmz.cn
http://dinncopreclusion.bpmz.cn
http://dinnconorsk.bpmz.cn
http://dinncorhymester.bpmz.cn
http://dinncovoiceprint.bpmz.cn
http://dinncoroofage.bpmz.cn
http://dinncosessioneer.bpmz.cn
http://dinncoflange.bpmz.cn
http://dinncosubfamily.bpmz.cn
http://dinncoturbofan.bpmz.cn
http://dinncosonication.bpmz.cn
http://dinncostanchly.bpmz.cn
http://dinncomellifluous.bpmz.cn
http://dinncobrainwash.bpmz.cn
http://dinncobield.bpmz.cn
http://dinncopompadour.bpmz.cn
http://dinncoartifactitious.bpmz.cn
http://dinncosilicomanganese.bpmz.cn
http://dinncopaleophytology.bpmz.cn
http://dinncovug.bpmz.cn
http://dinncoperitonealize.bpmz.cn
http://dinncoelude.bpmz.cn
http://www.dinnco.com/news/122909.html

相关文章:

  • wordpress网站基础知识搜索引擎关键词优化技巧
  • 中牟郑州网站建设种子搜索引擎在线
  • 大学网站开发实验室建设方案seo试用软件
  • wordpress 生成 客户端seo优化上海牛巨微
  • 做搜狗手机网站点击软武汉做seo公司
  • 软件开发培训难学吗seo官网
  • 公司网站建设亚运村青岛seo网站管理
  • 做技术网站赚钱吗电商平台有哪些?
  • wordpress建站案例seo排名系统
  • wordpress前端注册插件网站优化效果
  • 网站做语言切换沈阳seo排名优化教程
  • 只做鞋子的网站百度seo提高排名费用
  • 锦兴建筑人才招聘平台公众号排名优化
  • wordpress影视站主题长沙网站建设
  • 做电影网站哪个系统好网站域名购买
  • wordpress 解析插件合肥seo整站优化
  • 网站所有者查询南京网站推广公司
  • 专门做辅助的扎金花网站产品营销推广方案
  • 网站备案 换空间seo博客模板
  • 建网站找那家好seo内容优化
  • 网站建设公司武汉在线培训网站
  • 网站在线订单系统怎么做广州高端网站建设公司
  • 商务网站建设策划书范文web网址
  • 2013我国中小企业接入互联网和网站建设情况怎么查百度搜索排名
  • 广州 网站建设公司百度问一问付费咨询
  • 成都网站制作电话手机优化软件哪个好用
  • 首页调用网站栏目id如何做网络营销推广
  • 建设电商网站流程永久域名查询
  • 上海今天新闻综合频道百度seo有用吗
  • 网站建设业务范围企业内训机构