日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringSecurity实现拦截未登录页面

發布時間:2023/12/31 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringSecurity实现拦截未登录页面 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先引入依賴:

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

兩個前端頁面? 第一個login.html:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"><title>Insert title here</title> </head> <body>登錄頁面 <p th:text="${msg}"></p><form action="/login_check" method="post"><input type="text" name="username" /></br> </br> <input type="text"name="password" /></br> </br> <input type="submit" value="登錄" /></br></form></body> </html>

第二個頁面index.html

<!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body><form action="/login" method="post"><input type="submit" value="退出" /></br></form>你好啊! <p th:text="${user}"></p> </body> </html>

?java代碼:controller

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;@Controller public class IndexController {@RequestMapping("/index")@ResponseBodypublic ModelAndView index() {ModelAndView mode=new ModelAndView();mode.addObject("user", "qushen");mode.setViewName("index");return mode;}@RequestMapping("/login")public ModelAndView login() {ModelAndView mode=new ModelAndView();mode.addObject("msg", "后臺消息模板返回成功...");mode.setViewName("login");return mode;} }

下面就是?Security的java類配置文件:

首先先寫一個加密方式? MyPasswordEncoder.java:

import org.springframework.security.crypto.password.PasswordEncoder;public class MyPasswordEncoder implements PasswordEncoder{@Overridepublic String encode(CharSequence RawPassword) {return RawPassword.toString();}@Overridepublic boolean matches(CharSequence RawPassword, String EncodePassword) {return EncodePassword.equals(RawPassword.toString());}}

然后寫Security的主要配置文件:

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter{@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("qushen").password("123456").roles("ADMIN").and().passwordEncoder(new MyPasswordEncoder());}@Bean@Overrideprotected AuthenticationManager authenticationManager() throws Exception {return super.authenticationManager();}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable(); // 關閉跨站檢測http.authorizeRequests().anyRequest().fullyAuthenticated(); // 所有的請求全驗證http.formLogin().loginPage("/login").loginProcessingUrl("/login_check").failureUrl("/login").defaultSuccessUrl("/index").permitAll();http.logout().permitAll();}}

?

?

?

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的SpringSecurity实现拦截未登录页面的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。