一、HttpSecurity源碼
HttpSecurity類是Spring Security核心配置類之一,用來配置Spring Security的行為,包括認證方式、權限控制、CSRF保護等。我們先來看一下HttpSecurity類的源碼結構:
@Configuration
public final class HttpSecurity extends
AbstractConfiguredSecurityBuilder
implements SecurityBuilder, SecurityConfigurerAware, HttpSecurity>, HttpSecurityBuilder {
// 構造方法
public HttpSecurity(ObjectPostProcessor objectPostProcessor, AuthenticationManagerBuilder builder,
Map, Object> sharedObjects) {
super(objectPostProcessor, true, sharedObjects);
// come code here...
}
// 配置方法
public HttpSecurity requestMatcher(RequestMatcher requestMatcher) {
// some code here...
return this;
}
// some other methods...
}
如上代碼所示,HttpSecurity是一個@Configuration配置類,繼承了AbstractConfiguredSecurityBuilder類,并實現了SecurityBuilder和SecurityConfigurerAware接口。其中,requestMatcher方法用于匹配請求的URL,根據匹配結果來對請求進行處理。
二、HttpSecurity的用法
使用方法可以分為以下幾步:
1、創建一個SecurityConfigurer,并在其中重寫configure方法,實現對HttpSecurity的配置;
2、通過調用HttpSecurity的apply方法,并傳入第一步創建的SecurityConfigurer,來將SecurityConfigurer中的配置應用到HttpSecurity中;
3、配置成功后,HttpSecurity會返回一個DefaultSecurityFilterChain類型的對象,用于構建安全過濾器鏈。
三、HttpSecurity配置
3.1 HttpSecurity默認配置
當我們沒有對HttpSecurity進行額外的配置時,Spring Security會使用默認配置。默認配置包括以下內容:
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
以上代碼表示,所有請求需要進行身份驗證,并且要求用戶使用表單登錄或HTTP基本認證。
3.2 HttpSecurity權限配置詳解
針對不同路徑和請求類型,我們可以配置不同的授權策略。
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
以上代碼表示,對于"/admin/**"路徑下的請求,需要有"ADMIN"角色方可訪問;對于"/db/**"路徑下的請求,需要有"ADMIN"和"DBA"兩個角色方可訪問;而其他請求只需要在通過身份驗證后即可訪問。同時,我們還可以設置登錄頁和退出頁。
3.3 HttpSecurity解密失敗怎么回事
解密失敗可能是因為客戶端傳遞的數據被篡改或者密鑰不正確,當出現解密失敗的情況,可以使用以下代碼對HttpSecurity進行配置:
http.exceptionHandling()
.authenticationEntryPoint(new Http403ForbiddenEntryPoint())
.accessDeniedHandler(new AccessDeniedHandlerImpl());
以上代碼為設置Http403ForbiddenEntryPoint和AccessDeniedHandlerImpl類,用于處理解密失敗后的異常情況。
四、完整代碼示例
下面是一個完整的HttpSecurity配置示例:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.exceptionHandling()
.authenticationEntryPoint(new Http403ForbiddenEntryPoint())
.accessDeniedHandler(new AccessDeniedHandlerImpl());
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
以上代碼為一個完整的Spring Security配置類示例,其中包含了對HttpSecurity和AuthenticationManagerBuilder的配置。
tags: httpsecurity
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
上一篇
option125的全面解析
下一篇
linux獲取MAC地址,linux獲取dns
免費打包獲取
相關推薦HOT
libffi-devel詳解
一、介紹libffi是一個C語言庫,它提供了一些接口函數,使得在C語言中調用其他語言的代碼成為可能。要實現這一功能,libffi主要提供了以下幾個接...詳情>>
2023-11-23 16:40:27
用法介紹jsnewmap()
一、語法簡介jsnewmap()是JavaScript中的一種內置函數,它用于創建一個新的Map對象。它的語法如下:new Map([iterable])其中,iterable是一個可...詳情>>
2023-11-23 16:26:03
QTabWidget樣式詳解
一、qtablewidget樣式表1、背景顏色設置QTableWidget{ background-color: #F5F5F5; selection-background-color詳情>>
2023-11-23 16:15:15
linux 連接無線,linux配置無線
linux系統下怎么設置連wifisudo dhcpcd wlan0 如果一切順利的話,您應該已經通過 DHCP 獲取到了一個全新的本地 IP,這個過程是在后臺自動完成...詳情>>
2023-11-23 16:08:51
matplotlib畫布大小
一、默認畫布大小matplotlib是一個用Python編寫的繪圖庫,可以制作各種類型的圖表和可視化。在matplotlib中,畫布是你繪制圖形的區域,可以通過...詳情>>
2023-11-23 16:08:03
熱門推薦
C++中的write函數詳解
沸
Java 學生類與教師類詳解
熱
PageHelper依賴詳解
熱
linux刪除sh腳本的命令,linux刪除.sh
新
4
libffi-devel詳解
5
Swiper.js的全面解析
6
解析URL中的Query參數
7
如何強制刪除K8s中的Pod
8
用法介紹jsnewmap()
9
linux桌面快捷方式雙擊不能運行,linux桌面快捷方式不能打開
技術干貨
更多>>
如何實現服務器負載均衡
2023-12-06
linux有哪些優勢和劣勢
2023-12-06
linux需要驅動嗎
2023-12-06
android與linux的區別
2023-12-06
如何搭建基于容器的深度學習環境
2023-12-06
職場就業
更多>>
網絡安全軟件開發的就業前景
2023-12-09
學會python工程師后的就業前景
2023-12-09
學會java工程師后的就業前景
2023-12-09
云計算技術就業前景以及發展方向怎樣?
2023-08-07
快速通道
培訓機構
了解培訓相關
就業前景
查看就業前景
培訓門檻
了解學習門檻
應聘面試
常見面試考題
就業服務
畢業推薦就業
師資團隊
了解師資團隊
初心至善 匠心育人
400-811-9990
24小時在線咨詢
關于千鋒
千鋒簡介
聯系我們
企業服務
鋒益公益
學習資源
項目庫
公開課
視頻教程
服務指南
報名咨詢
校區分布
網站地圖
隱私聲明
千鋒學習站 | 隨時隨地免費學
掃一掃進入千鋒手機站
熱門課程
IT培訓
java培訓
鴻蒙開發培訓
嵌入式培訓
python培訓
UI培訓
軟件測試培訓
云計算培訓
大數據培訓
物聯網培訓
游戲開發培訓
全媒體運營培訓
影視剪輯培訓
網絡安全培訓
Copyright 2011-2025
北京千鋒互聯科技有限公司
京ICP備12003911號-3
京公網安備 11010802030320號
千鋒教育 運營主體:北京千鋒互聯科技有限公司,屬具備計算機技術培訓資質的教育培訓機構。
hljs.highlightAll();
$(document).ready(function () {
var href = $("#tag_click").attr("href");
if (href && href.indexOf("-0-") != -1) {
var tag_code = $("#tag_click").html();
$.ajax({
url: "http://app.mobiletrain.org/tags.php",
type: "get",
dataType: "json",
data: { tag_code: tag_code },
success: function (data) {
$("#tag_click").attr(
"href",
"http://www.kei0345678.cn/tag-" + data + "-1.html"
);
},
});
}
});
if (
location.href.indexOf("/about/info/") > -1 ||
location.href.indexOf("/about/news/") > -1
) {
document
.getElementsByClassName("topicactive")[0]
.classList.remove("topicactive");
document
.getElementsByClassName("interviewactive")[0]
.classList.remove("interviewactive");
} else if (location.href.indexOf("/interview/") > -1) {
document
.getElementsByClassName("topicactive")[0]
.classList.remove("topicactive");
document
.getElementsByClassName("xwzxactive")[0]
.classList.remove("xwzxactive");
} else {
document
.getElementsByClassName("xwzxactive")[0]
.classList.remove("xwzxactive");
document
.getElementsByClassName("interviewactive")[0]
.classList.remove("interviewactive");
}
new Swiper(".swiper1", {
loop: true,
autoplay: true,
pagination: {
el: ".swiper-pagination1",
},
});
// new Swiper(".swiper3", {
// slidesPerView: 14,
// slidesPerGroup: 14,
// spaceBetween: 20,
// loop: true,
// direction: "vertical",
// // navigation: {
// // nextEl: ".swiper-button-next3",
// // prevEl: ".swiper-button-prev3",
// // },
// });
// new Swiper(".swiper4", {
// slidesPerView: 6,
// slidesPerGroup: 6,
// spaceBetween: 21,
// loop: true,
// direction: "vertical",
// navigation: {
// nextEl: ".swiper-button-next4",
// prevEl: ".swiper-button-prev4",
// },
// });
// new Swiper(".swiper-phone", {
// slidesPerView: 4,
// autoplay: true,
// spaceBetween: 0,
// loop: true,
// direction: "vertical",
// });
// $(".select_txt,.selet_open").hover(function (event) {
// $(".option").hide();
// $(this).siblings(".option").show();
// $(".select_box").removeClass("uiChooseActiveS");
// $(this).parent(".select_box").addClass("uiChooseActiveS");
// });
// $(document).click(function (event) {
// var eo = $(event.target);
// if (
// $(".select_box").is(":visible") &&
// eo.attr("class") != "option" &&
// !eo.parent(".option").length
// )
// $(".option").hide();
// $(".select_box").removeClass("uiChooseActiveS");
// });
// $(".option a").click(function () {
// var value = $(this).text();
// $(this).parent().siblings(".select_txt").text(value);
// $(".select_value").val(value);
// $(".option").hide();
// $(".select_box").removeClass("uiChooseActiveS");
// });
// $(".option").each(function (d) {
// var optionSize = 6;
// var optionLiHeight = $(".option a").height();
// var optionVarS = $(this).find("a").length - 2;
// console.log(optionVarS, optionSize);
// if (optionVarS > optionSize) {
// var optionSheight = 175;
// $(this).height(optionSheight);
// } else {
// var optionLiTal = optionVarS * optionLiHeight;
// $(this).height(optionLiTal);
// }
// });
// $(".option,.uiChooseTable,.mod_select ").mouseleave(function () {
// $(".select_box").removeClass("uiChooseActiveS");
// $(".option").hide();
// });
// $(".option a").each(function () {
// $(this).html("第" + $(this).text() + "頁");
// });
new Swiper(".swiper-news", {
autoplay: true,
loop: true,
direction: "vertical",
});
$(".footer-link .links-tab span").click(function () {
$(this).addClass("active").siblings().removeClass("active");
$(".footer-link .links-a").removeClass("active");
$(".footer-link .links-a").eq($(this).index()).addClass("active");
});
// 開班信息模塊
function kbxx () {
// var courseId =
// arguments.length > 0 && arguments[0] !== undefined
// ? arguments[0]
// : 13;
var cityId = arguments[0] !== undefined ? arguments[0] : 10;
$.ajax({
url: "https://owzsapi.qfedu.com/v1/api/openApiRestController/getAllClass?type=1",
type: "GET",
success: function success (res) {
var r = "";
var curTime = new Date().getTime();
var filterArr = res.data.filter(function (v) {
return v.cityId == cityId;
});
console.log(filterArr);
filterArr.reverse().forEach(function (v, idx) {
// 最大條數
if (idx > 14) return;
// 判斷時間
var timeMode = "預約占座";
var kbTime = new Date(v.beginTime).getTime();
// 一個月
if (kbTime - curTime > 2592000000) {
timeMode = "預約占座";
}
if (kbTime - curTime > 0 && kbTime - curTime < 2642268030) {
timeMode = "即將報滿";
}
if (kbTime - curTime < 0) {
timeMode = "爆滿開班";
}
r +=
"<li id="oau0tmy" class='swiper-slide'>\n <a href='javascript:;' rel='nofollow' onclick='open53_pc()'>\n <span>" +
(v.name.replace(v.cityName, "").split("班")[0] + "班") +
"</span>\n <span>" +
v.beginTime +
"</span>\n <span data-mode='" +
(timeMode === "即將報滿"
? 0
: timeMode === "爆滿開班"
? 2
: 1) +
"'>" +
timeMode +
" " +
(timeMode === "即將報滿" ? "" : "") +
"</span>\n </a>\n </li>";
});
$(".des-right-classes .swiper-wrapper").html(r);
new Swiper(".swiper6", {
slidesPerView: 5,
slidesPerGroup: 5,
spaceBetween: 10,
loop: true,
autoplay: true,
direction: "vertical",
});
},
});
}
kbxx(10);
$(".kbxx_showXq_btn").click(function () {
$(".kbxx_showXq_box").css("display", "block");
});
$(".kbxx_showXq_box ul li").click(function () {
kbxx($(this).data("cityid"));
$(".choose-xq").html($(this).context.innerText);
$(".kbxx_showXq_box").css("display", "none");
});
/******************表單提交start********************/
// (function () {
// var timer = setInterval(function () {
// if (window.$) {
// clearInterval(timer);
// $(".list-submit").click(function () {
// var params = {
// name: $("#realname").val(),
// phone: $("#phone").val(),
// };
// var phReg = /^[\u4e00-\u9fa5a-zA-Z0-9-]{2,20}$/;
// var nameReg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]8$/;
// // if (!nameReg.test($("#realname").val())) {
// // alert("請正確輸入姓名");
// // return;
// // }
// if (!phReg.test($("#phone").val())) {
// alert("請正確輸入手機號");
// return;
// }
// $.ajax({
// type: "POST",
// contentType: "application/json",
// url:
// "http://api-newzs.1000phone.net//v1/api/seoFormController/add",
// data: JSON.stringify(params),
// success: function success(res) {
// alert(res.msg);
// },
// });
// });
// }
// }, 1000);
// })();
/******************表單提交end********************/
console.log($(".content-left-box-child").height());
console.log($(".content-right-box-child").height());
if (
$(".content-left-box-child").height() >
$(".content-right-box-child").height()
) {
$(".content-right-box-child").addClass("sticky");
} else if (
$(".content-left-box-child").height() <
$(".content-right-box-child").height()
) {
$(".content-left-box-child").addClass("sticky");
}
// var bot =
// $(".con-right-rywd").offset().top +
// $(".con-right-rywd").outerHeight() -
// $(window).height() +
// 30;
// console.log(bot);
// var left = $("#fixTop").offset().left;
// $(document).scroll(function () {
// var scroH = $(document).scrollTop();
// var footTop = $(".edu-footer").offset().top - $(window).height();
// if (scroH > bot) {
// $("#fixTop").addClass("rig_fixed");
// $("#fixTop").css({ left: left });
// if (scroH > footTop) {
// $("#fixTop").addClass("rig_bottom");
// $("#fixTop").css({ left: "909px" });
// } else {
// $("#fixTop").removeClass("rig_bottom");
// // $("#fixTop").css({ left: "auto" });
// }
// } else {
// $("#fixTop").removeClass("rig_fixed");
// $("#fixTop").removeClass("rig_bottom");
// // $("#fixTop").css({ left: "auto" });
// }
// });
感谢您访问我们的网站,您可能还对以下资源感兴趣:
国产一区二区精品-国产一区二区精品久-国产一区二区精品久久-国产一区二区精品久久91-免费毛片播放-免费毛片基地
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else {
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
<th id="nn1ly"><ul id="nn1ly"></ul></th><ins id="nn1ly"><option id="nn1ly"></option></ins><address id="nn1ly"><option id="nn1ly"></option></address>