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

网站前台用什么开发产品宣传推广方案

网站前台用什么开发,产品宣传推广方案,短链接恢复长连接,项目立项查询平台一、前言 Spring Security已经更新到了6.x,通过本专栏记录以下Spring Security6学习过程,当然大家可参考Spring Security5专栏对比学习 Spring Security5专栏地址:security5 Spring Security是spring家族产品中的一个安全框架,核心功能包括…

一、前言

Spring Security已经更新到了6.x,通过本专栏记录以下Spring Security6学习过程,当然大家可参考Spring Security5专栏对比学习

Spring Security5专栏地址:security5

Spring Security是spring家族产品中的一个安全框架,核心功能包括用户认证(Authentication)和用户授权(Authorization)两部分。

用户认证:验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。

用户授权:指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。就是所谓的RBAC系统。

权限认证框架类似的还有shiro、以及国产Sa-Token,可以类比学习
 

二、项目创建

创建一个springboot项目,jdk版本17、boot版本3.1.2、security版本6

pom.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.2</version><relativePath/></parent><groupId>com.demo</groupId><artifactId>security6</artifactId><version>0.0.1-SNAPSHOT</version><name>security6</name><description>security6</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

自定义一个controller

@RestController
public class IndexController {@RequestMapping("index")public String index(){return "index";}}

启动项目访问index路径,可以看到securit已经拦截请求

 使用控制台提供的默认密码即可登录

 实际是security底层有个拦截器,拦截判断是否登录,没有的话会跳转到登录login页面

当然上面的用户名和密码可以通过配置改变,如下

spring.security.user.name=admin
spring.security.user.password=12345

三、前后端不分离项目自定义相关配置

首先引入前后端不分离页面模板引擎Thymeleaf依赖

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

让后resources/templates下创建登录页面和首页

登录页面login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>登录</title>
</head>
<body>
<h1>欢迎登录</h1>
<form th:action="@{/login}" method="post">用户名:<input type="text" name="username">密码: <input type="text" name="password"><input type="submit" value="登录">
</form>
</body>
</html>

首页页面index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
<h1>欢迎使用本系统</h1>
注意:security退出是一个post请求
<form method="post" th:action="@{/logout}"><input type="submit" value="退出">
</form>
</body>
</html>

controller类

@Controller
public class IndexController {@RequestMapping("info")@ResponseBodypublic String info(){return "欢迎使用本系统!";}//前往index页面@RequestMapping("index")public String index(){return "index";}//前往登录页面@RequestMapping("toLoginPage")public String login(){return "login";}
}

和5的使用方法一样,修改security相关配置还是通过自定义配置类,如下

/*** 5版本只需@Configuration一个注解,不需要@EnableWebSecurity,* 6需要同时引入,并且5是需要extends WebSecurityConfigurerAdapter类*/
@Configuration
@EnableWebSecurity
public class MySecurityConfig {/*** 5版本是override 方法: configure(HttpSecurity http),6是下面bean*/@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{//authorizeHttpRequests:针对http请求进行授权认证,定义不需要认证就能的资源路径,如infohttp.authorizeHttpRequests(authorizeHttpRequests->authorizeHttpRequests.requestMatchers("/info").permitAll().anyRequest().authenticated());/**登录表单配置* loginPage:登录页面请求地址* loginProcessingUrl:登录接口 过滤器* successForwardUrl:登录成功响应地址* failureForwardUrl:登录失败响应地址*/http.formLogin(formLogin->formLogin.loginPage("/toLoginPage").permitAll().loginProcessingUrl("/login").successForwardUrl("/index").failureForwardUrl("/toLoginPage"));//关闭crsf 跨域漏洞防御http.csrf(withDefaults());//相当于 http.csrf(Customizer.withDefaults());或者http.csrf(crsf->crsf.disable());//退出http.logout(logout -> logout.invalidateHttpSession(true));return http.build();}
}

四、前后端分离项目登录

对于前后端分离项目,我们不再需要模板引擎thymeleaf依赖以及index,login页面,核心配置类如下

@Configuration
@EnableWebSecurity
public class MySecurityConfig {@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{//不需要认证的请求http.authorizeHttpRequests(authorizeHttpRequests->authorizeHttpRequests.requestMatchers("/info").permitAll().anyRequest().authenticated());//登录http.formLogin(formLogin -> formLogin//登录接口.loginProcessingUrl("/login")//登录成功响应,直接使用接口表达式,也可和5版本一样子自定义类.successHandler((request,response,authentication)->{//这里可以根据自己业务封装响应体response.setContentType("text/html;charset=UTF-8");response.getWriter().write("ok");System.out.println(authentication.getCredentials()+"--"+authentication.getPrincipal()+"--"+authentication.getAuthorities());})//登录失败响应.failureHandler((request,response,authenticationException) ->{response.setContentType("text/html;charset=UTF-8");response.getWriter().write("ok");authenticationException.printStackTrace();}));//关闭crsf,跨域漏洞防御http.csrf(withDefaults());//跨域拦截http.cors(withDefaults());return http.build();}
}

http://www.hkea.cn/news/254478/

相关文章:

  • xampp安装wordpress说明徐州seo外包
  • 啥网站都能看的浏览器下载百度收录查询工具
  • 福田附近公司做网站建设哪家效益快奶糖 seo 博客
  • 临沂免费自助建站模板品牌整合营销
  • iis做本地视频网站找客户资源的网站
  • 做调查用哪个网站网络推广有多少种方法
  • 开发一个交易网站多少钱在线工具
  • 网站平台怎么建立的软文范例
  • 移动应用开发专业学什么东莞seo软件
  • 做宣传网站的公司手机百度极速版app下载安装
  • 私人可以做慈善网站吗外贸如何推广
  • 网站页面模板页面布局如何成为百度广告代理商
  • 瑞安外贸网站建设曲靖百度推广
  • 先做网站还是服务器销售营销方案100例
  • 用卫生纸做的礼物街网站免费网页空间到哪申请
  • 手游网站做cpc还是cpm广告号厦门网页搜索排名提升
  • 人个做外贸用什么网站好宁波百度seo点击软件
  • 诈骗网站怎么做的企业网站seo案例分析
  • 如何做网站接口湖南营销型网站建设
  • 进入兔展网站做PPt软文营销ppt
  • app网站新闻危机公关
  • 东莞关键词优化实力乐云seo南宁seo外包服务商
  • 做网站都是用源码么免费注册个人网站不花钱
  • 建设网站需要两种服务支持官网设计公司
  • 安庆做网站seo建站收费地震
  • 绵阳住房和城市建设局网站官网seo排名优化联系13火星软件
  • 网站开发建设费用关键词异地排名查询
  • 网站建设企业电话广州优化疫情防控举措
  • 重庆模板网站建设百度网站域名注册
  • 安徽建设厅网站地址网络广告推广方式