首先你得在pom.xml中导入相应的依赖,再写一个类来继承websecuityconfigurerAdapter在这个类上写上注解@Enablewebsecurity
重写 configure(HttpSecurity http)这是授权 里面的方法http.authorizeRequests.permitAll(这这是全部可以访问).antMatchers(“访问的页面”).hsrole(“所能访问的角色”) ;
http.formlogin()会使你直接跳转到登录页面 代码会自动判断.loginpage("/会跳转到你所需要的指定页面") .loginprocessingUrl("/你真实要走的页面").usernameparameter(“所对因的前端传来的值”)在源码里面有默认的.passwordparameter(“所对因的前端传来的值")在源码里面有默认的值;
http.logout() 开启了注销功能.logoutSuccessUrl("/跳转到你所需要的页面")
http.csrf().disable();防止网站攻击 get不安全的 post 安全的但是只能 form表单来进行交互 这个主要是拿来关闭csrf功能;
http.rememberme();简单的实现登录记住我功能 本质是一个cooke 默认两周;
重写 configure(AuthentionManageBulider auth) 这是认证 auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())withUser(“用户名”),password(new BCryptPasswordEncoder().encoder(“密码”)).roles(“角色”)
Spring Security 的使用
最新推荐文章于 2023-05-15 20:23:41 发布