springboot的WEB开发笔记(SpringSecurity)
2020-08-10 20:10
1071 查看
SpringSecurity
SpringSecurity
SpringSecurity官方使用文档:https://docs.spring.io/spring-security/site/docs/5.2.6.RELEASE/reference/htmlsingle/#jc
依赖添加
SpringSecurity配置的版本会受springboot的版本影响
thymeleaf-extras-springsecurity5是适用于2.0.9.RELEASE以上的
thymeleaf-extras-springsecurity4是适用于2.0.9.RELEASE以下的
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> <version>3.0.4.RELEASE</version> </dependency>
页面
页面适用thymeleaf-extras-springsecurity的配置
//thymeleaf-extras-springsecurity4的页面配置 <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"> //thymeleaf-extras-springsecurity5的页面配置 <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
sec的运用
//sec:authorize="!isAuthenticated()"进行一个是否登陆认证的验证,登陆认证则不显示 <div sec:authorize="!isAuthenticated()"><a th:href="@{/toLogin}">登陆</a></div> //获取登陆用户的用户名 <span sec:authentication="name"> //获取登陆用户的权限,principal.authorities不可写成principal.getAuthorities() <span sec:authentication="principal.authorities"> //根据用户权限判断是否显示 <div sec:authorize="hasRole('vip1')"></div>
授权
①进行WebSecurityConfigurerAdapter的扩展实现
②可以用http进行许多功能的配置
@EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { /** * 授权 * @param http * @throws Exception */ @Override protected void configure(HttpSecurity http) throws Exception { //请求授权的规则:首页都可以访问,其他页面需要对应权限 http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2"); //没有权限就会到权限登陆页面 http.formLogin(); //注销 http.logout().logoutSuccessUrl("/"); //开启记住我 http.rememberMe(); } }
授权的登陆页面为自定页面的操作有两种
①直接修改SecurityConfig类和页面表单提交的action,但这种方法具有缺点就是name=username/password需固定
http.formLogin().loginPage("/toLogin");
<form th:action="@{/toLogin}" method="post">
②loginPage为没有授权就会返回的登陆页面,loginProcessingUrl为处理页面的接口,usernameParameter,passwordParameter是网页传入的name值需要相对应
http.formLogin().loginPage("/toLogin").usernameParameter("username").passwordParameter("password").loginProcessingUrl("/login");
授权的自定义rememberMe
http.rememberMe().rememberMeParameter("remember");
<input type="checkbox" name="remember">remember Me
权限认证
springboot 2.1.X 可以直接使用
2.2.X以上的需要进行password的加密,以防反编译程序进行一个密码获取
/** * 认证 * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("vip1") .and() .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2"); }
相关文章推荐
- springboot的WEB开发笔记(Shiro)
- SpringBoot18-springboot的Web开发-Tomcat配置
- 《PHP MySQL和WEB开发》笔记第十一章
- http协议 web开发的应用 笔记
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-001-SpringSecurity简介(DelegatingFilterProxy、AbstractSecurityWebApplicationInitializer、WebSecurityConfigurerAdapter、@EnableWebSecurity、@EnableWebMvcS)
- springboot学习笔记-5 springboot整合shiro
- Java的web开发中SSH框架的协作处理应用笔记
- web开发笔记(待整理)
- java笔记:SpringSecurity应用(二)
- 【努力学习】SpringBoot 笔记 0.3(IOC操作Bean基于注解方式实现)
- Spring Boot学习笔记:(三)Web开发(Thymeleaf)
- SpringBoot的SpringMVC视图解析器源码学习笔记
- SpringBoot自学笔记(一)
- 【web开发学习笔记】Structs2 OGNL学习笔记
- SpringBoot 下 SpringSecurity 入门搭建
- springboot开发入门笔记-HelloSpringboot
- SpringBoot集成SpringSecurity遇到的坑
- Django Web开发学习笔记(3)
- SpringSecurity学习笔记之一:SpringSecurity概述及Github项目克隆
- ASP.NET 学习笔记_04 Session、http、web开发原则、xss漏洞