SpringBoot-Security 使用

功能:解决web站点的登录,权限验证,授权等功能

优点:在不影响站点业务代码,可以权限的授权与验证横切到业务中

1、要添加的依赖

 1     <!--thymeleaf-->
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter-thymeleaf</artifactId>
 5         </dependency>
 6         <!--security 和 thymeleaf 整合包-->
 7         <dependency>
 8             <groupId>org.thymeleaf.extras</groupId>
 9             <artifactId>thymeleaf-extras-springsecurity5</artifactId>
10         </dependency>
11         <!--web-->
12         <dependency>
13             <groupId>org.springframework.boot</groupId>
14             <artifactId>spring-boot-starter-web</artifactId>
15         </dependency>
16         <!--security-->
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter-security</artifactId>
20         </dependency>

2、Security 下授权与验证的简单配置(Security下有登录,注销,记住我等功能,可以快速集成到自己的login页上)

Tis:如果template页中使用了 Frame页,默认是不能访问的,需要添加 http.headers().frameOptions().sameOrigin();
 1 @EnableWebSecurity
 2 public class SecurityConfig extends WebSecurityConfigurerAdapter {
 3     //授权
 4     @Override
 5     protected void configure(HttpSecurity http) throws Exception {
 6 
 7         //请求授权的规则
 8         http.authorizeRequests()
 9                 //.antMatchers("/tologin").permitAll() //登录页所有人都可以访问
10                 //.antMatchers("/admin/**").hasRole("admin1")
11                 .antMatchers("/admin/list").hasRole("admin1")
12                 .antMatchers("/admin/role").hasRole("admin1")
13                 .antMatchers("/admin/cate").hasRole("admin2")
14                 .antMatchers("/admin/rule").hasRole("admin2");
15 
16         // 项目里面使用了springSecurity spring Security下,X-Frame-Options默认为DENY,非spring Security环境下,X-Frame-Options的默认大多也是DENY,这种情况下,浏览器拒绝当前页面加载任何Frame页面
17         http.headers().frameOptions().sameOrigin();
18 
19         //登录页(Security默认有一个登录页)
20         http.formLogin().permitAll()
21                 .loginPage("/tologin") //指定自定义的登录页地址
22                 .successForwardUrl("/admin/index") //登录成功跳转地址
23                 .usernameParameter("username").passwordParameter("password");//匹配自定义登录页的name元素名称
24 
25         // 开启注销功能,跳转到登录页
26         http.csrf().disable(); //退出失败可能能的原因
27         http.logout().logoutSuccessUrl("/tologin");
28 
29         //开启记住我功能,cookie 默认保存14天
30         http.rememberMe()
31                 .rememberMeParameter("remember");//匹配自定义登录页的name元素名称
32 
33     }
34 
35     //认证
36     @Override
37     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
38         auth.inMemoryAuthentication()
39                 .passwordEncoder(new BCryptPasswordEncoder())//密码加密方式(有些版本的Security必须要指定)
40                 .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("admin1","admin2","admin3")
41                 .and()
42                 .withUser("yeqiu").password(new BCryptPasswordEncoder().encode("123")).roles("admin1")
43                 .and()
44                 .withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("admin2");
45 
46     }
47 }

 

3、Security 和 Thymeleaf 页面整合(添加依赖:thymeleaf-extras-springsecurity)

<!--加入约束-->
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

<!--
    sec:authorize="isAuthenticated()" 用户是否登录
    sec:authorize="hasAnyRole('admin1')" 是否具有某个角色
    sec:authentication="name" 当前登录用户
    sec:authentication="principal.authorities" 当前用户全部角色
-->

<div sec:authorize="isAuthenticated()">
    <h2><span sec:authentication="name"></span>,您好 您的身份是
        <span sec:authentication="principal.authorities"></span>
    </h2>
</div>

<li sec:authorize="hasRole('admin2')">
      <a onclick="xadmin.add_tab('权限管理','admin/rule')">
            <cite>权限管理</cite>
      </a>
</li> 

 

posted @ 2020-11-20 11:06  人间有妖气  阅读(171)  评论(0编辑  收藏  举报