當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringSecurity实现拦截未登录页面
生活随笔
收集整理的這篇文章主要介紹了
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实现拦截未登录页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王者怎么隐身登录游戏
- 下一篇: 手动搭建一个Spring Boot项目