首页 技术 正文
技术 2022年11月11日
0 收藏 345 点赞 3,872 浏览 7835 个字

Spring Security 整合freemaker 实现简单登录和角色控制

  

写这篇文章是因为我做了一个电商网站项目,近期刚加上权限控制。整个过程很简单,在此给大家梳理一下,也算是自己对知识点的一个总结。

一、需求分析:

我们都知道,电商网站在权限这一块,有两大块内容:

1、用户未登录,部分页面拒绝访问(如:下订单)

2、不同角色用户登录看到的功能模块不一样(如:买家、卖家、客服等)

基于以上需求,接下来我们要解决的就是对用户登录的拦截以及对权限和角色的控制。

二、项目环境说明:

使用SSM(SpringMVC+Spring+Mybatis)框架,mysql数据库、maven项目管理工具,freemaker前端引擎。对以上又不懂的朋友们可以自己去百度了解,这里就废话不多说了。

三、前期储备知识(如果对Spring Security很熟悉的可以跳过此步)

Security框架可以精确控制页面的一个按钮、链接,它在页面上权限的控制实际上是通过它提供的标签来做到的。

  • 简介

一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方式的安全框架(简单说是对访问权限进行控制嘛),应用的安全性包括用户认证(Authentication)用户授权(Authorization)两个部分。用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。用户授权指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。spring security的主要核心功能为认证和授权,所有的架构(如:Shiro安全框架)也是基于这两个核心功能去实现的。

  • 框架原理

众所周知 想要对对Web资源进行保护,最好的办法莫过于Filter,要想对方法调用进行保护,最好的办法莫过于AOP。所以springSecurity在我们进行用户认证以及授予权限的时候,通过各种各样的拦截器来控制权限的访问,从而实现安全。

如下为其主要过滤器  :

WebAsyncManagerIntegrationFilter 
        SecurityContextPersistenceFilter 
        HeaderWriterFilter 
        CorsFilter 
        LogoutFilter
        RequestCacheAwareFilter
        SecurityContextHolderAwareRequestFilter
        AnonymousAuthenticationFilter
        SessionManagementFilter
        ExceptionTranslationFilter
        FilterSecurityInterceptor
        UsernamePasswordAuthenticationFilter
        BasicAuthenticationFilter

  • 框架的核心组件

SecurityContextHolder:提供对SecurityContext的访问
      SecurityContext,:持有Authentication对象和其他可能需要的信息
      AuthenticationManager 其中可以包含多个AuthenticationProvider
      ProviderManager对象为AuthenticationManager接口的实现类
      AuthenticationProvider 主要用来进行认证操作的类 调用其中的authenticate()方法去进行认证操作
      Authentication:Spring Security方式的认证主体
     GrantedAuthority:对认证主题的应用层面的授权,含当前用户的权限信息,通常使用角色表示
     UserDetails:构建Authentication对象必须的信息,可以自定义,可能需要访问DB得到
     UserDetailsService:通过username构建UserDetails对象,通过loadUserByUsername根据userName获取UserDetail对象

以上知识点来源于博客:springSecurity安全框架的学习和原理解读

四、开始实战:

本文内容主要实现三部分内容:

1、控制不同角色只能访问网站中不同链接(与2作区分)

2、控制不同角色用户看到网站中不同的模块

3、对有权限控制的路径,控制用户session过期时重新跳转到登录页面

  • 在pom.xml文件中加入Security 坐标:
  1.   <!–spring security 依赖包–>
  2.   <dependency>
  3.   <groupId>org.springframework.security</groupId>
  4.   <artifactId>spring-security-core</artifactId>
  5.   <version>3.2.0.RELEASE</version>
  6.   </dependency>
  7.   <dependency>
  8.   <groupId>org.springframework.security</groupId>
  9.   <artifactId>spring-security-web</artifactId>
  10.   <version>3.2.0.RELEASE</version>
  11.   </dependency>
  12.   <dependency>
  13.   <groupId>org.springframework.security</groupId>
  14.   <artifactId>spring-security-config</artifactId>
  15.   <version>3.2.0.RELEASE</version>
  16.   </dependency>
  17.   <dependency>
  18.   <groupId>org.springframework.security</groupId>
  19.   <artifactId>spring-security-taglibs</artifactId>
  20.   <version>3.2.0.RELEASE</version>
  21.   </dependency>
  • 在web.xml中配置Security
  1.   <!–配置Spring Security–>
  2.   <!–filter的声明–>
  3.   <filter>
  4.   <filter-name>springSecurityFilterChain</filter-name>
  5.   <filter-class>
  6.   org.springframework.web.filter.DelegatingFilterProxy
  7.   </filter-class>
  8.   </filter>
  9.   <!–mapping就是filter的映射,就是哪些文件用到这个filter–>
  10.   <filter-mapping>
  11.   <filter-name>springSecurityFilterChain</filter-name>
  12.   <url-pattern>/*</url-pattern>
  13.   </filter-mapping>
  • Controller文件代码 (SecurityConfig.java)
  1.  @Configuration
  2.  @EnableWebSecurity
  3.  @Component
  4.  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  5.   
  6.   @Autowired
  7.   private UserDetailService userDetailService;
  8.   
  9.   @Override
  10.   protected void configure(HttpSecurity http) throws Exception{
  11.   http.authorizeRequests()
  12.   .antMatchers(“/index/show”).hasAnyRole(“ADMIN”,”BUYER”,”SELLER”)//个人首页只允许拥有ADMIN,BUYER,SELLER角色的用户访问
  13.   .antMatchers(“/cart/show”).hasAnyRole(“ADMIN”,”MAIJIA”,”SELLER”)
  14.   //在此后面可以根据自己的项目需要进行页面拦截的添加
  15.   .anyRequest().authenticated()
  16.   .and()
  17.   .formLogin()
  18.   .loginPage(“/index/login”).permitAll()//这里程序默认路径就是登陆页面,允许所有人进行登陆
  19.   .loginProcessingUrl(“/j_spring_security_check”)//登陆提交的处理url
  20.   .usernameParameter(“j_username”)//登陆用户名参数
  21.   .passwordParameter(“j_password”)//登陆密码参数
  22.   .failureUrl(“/index/login?error=true”)//登陆失败进行转发,这里回到登陆页面,参数error可以告知登陆状态
  23.   .defaultSuccessUrl(“/index/show”)//登陆成功的url,这里去到个人首页
  24.   .and().logout().logoutUrl(“/j_spring_security_logout”).permitAll().logoutSuccessUrl(“/index/login?logout=true”)//按顺序,第一个是登出的url,security会拦截这个url进行处理,所以登出不需要我们实现,第二个是登出url,logout告知登陆状态
  25.   .and()
  26.   .addFilter(myUsernamePasswordAuthenticationFilter)
  27.   .rememberMe()
  28.   .tokenValiditySeconds(604800)//记住我功能,cookies有限期是一周
  29.   .and()
  30.   .csrf().disable();
  31.   }
  32.   
  33.   @Override
  34.   public void configure(WebSecurity web) throws Exception{
  35.   super.configure(web);
  36.   }
  37.   
  38.   @Override
  39.   public void configure(AuthenticationManagerBuilder auth) throws Exception{
  40.   auth.userDetailsService(userDetailService);
  41.   }
  42.   
  43.   @Override
  44.   public AuthenticationManager authenticationManagerBean() throws Exception {
  45.   return super.authenticationManagerBean();
  46.   }
  47.  }
  • 接下来是Service层的代码(UserDetailService.java)
  1.  @Service
  2.  public class UserDetailService implements UserDetailsService {
  3.   
  4.   @Autowired
  5.   private UserDetailsDao userDetailsDao;
  6.   
  7.   /**
  8.   * 获取所属角色
  9.   */
  10.   public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  11.   
  12.   //查出用户名、密码、角色信息
  13.   Users users = userDetailsDao.getUserByName(username);
  14.   
  15.   if (users==null) {
  16.   throw new UsernameNotFoundException(“找不到该账户信息!”);
  17.   }
  18.   
  19.   List<GrantedAuthority> list = new ArrayList<GrantedAuthority>(); //GrantedAuthority是security提供的权限类,
  20.   list.add(new SimpleGrantedAuthority(“ROLE_”+users.getRoles()));
  21.   
  22.   User auth_user = new User(users.getUsername(), users.getPassword(), list);
  23.   
  24.   return auth_user;
  25.   
  26.   }
  27.   
  28.  }
  • 前端代码 .ftl文件(在此只粘出一部分代码,只显示一下用法)

在ftl文件头部加上,引入Security文件

<#assign sec=JspTaglibs["http://www.springframework.org/security/tags"]/>

 

  1.  <#–没有登录时 能看到买家、卖家所有信息–>
  2.  <@sec.authorize ifNotGranted=”ROLE_ADMIN,ROLE_MAIJIA,ROLE_SELLER,ROLE_BOTHSM”>
  3.  <dl>
  4.  <dt><a href=”/jt/success/orderlist?page=1&orderPeriod=threemonth&orderType=allorder”>买家中心</a></dt>
  5.  <dd>
  6.  <a href=”/jt/success/orderlist?page=1&orderPeriod=threemonth&orderType=allorder”>全部订单</a>   
  7.  <a href=”#”>优惠券</a> <br />
  8.  <a href=”/jt/cart/show” class=”shoppingCart”>我的购物车</a>  
  9.  <a href=”/jt/favorite/myfavorites/1/time”>我的收藏</a>
  10.  </dd>
  11.  </dl>
  12.   
  13.  <dl>
  14.  <dt><a href=”/jt/sellercenter/allproduct?page=1&fenleiId=allproduct”>卖家中心</a> </dt>
  15.  <dd>
  16.  <a href=”/jt/seller/soldprolist?page=1&orderPeriod=threemonth&orderType=allorder”>已卖出货品</a>  
  17.  <a href=”/jt/release/release1″>发布供应产品</a><br />
  18.  <a href=”/jt/sellercenter/allproduct?page=1&fenleiId=allproduct”>管理供应产品</a>   
  19.  <a href=”/jt/news/toNewsReleaseListSeller?currentPage=1&newsStatus=2″>发布公告</a>
  20.  </dd>
  21.  </dl>
  22.  </@sec.authorize>
  23.   
  24.  <#–登陆后,买家中心 只有以买家身份登录可以看到–>
  25.  <@sec.authorize ifAnyGranted=”ROLE_MAIJIA”>
  26.  <dl>
  27.  <dt><a href=”/jt/success/orderlist?page=1&orderPeriod=threemonth&orderType=allorder”>买家中心</a></dt>
  28.  <dd>
  29.  <a href=”/jt/success/orderlist?page=1&orderPeriod=threemonth&orderType=allorder”>全部订单</a>   
  30.  <a href=”#”>优惠券</a> <br />
  31.  <a href=”/jt/cart/show” class=”shoppingCart”>我的购物车</a>  
  32.  <a href=”/jt/favorite/myfavorites/1/time”>我的收藏</a>
  33.  </dd>
  34.  </dl>
  35.  </@sec.authorize>
  36.   
  37.  <#–登陆后,卖家中心 只有以卖家身份登录可以看到–>
  38.  <@sec.authorize ifAnyGranted=”ROLE_SELLER”>
  39.  <dl>
  40.  <dt><a href=”/lbt/sellercenter/allproduct?page=1&fenleiId=allproduct”>卖家中心</a> </dt>
  41.  <dd>
  42.  <a href=”/lbt/seller/soldprolist?page=1&orderPeriod=threemonth&orderType=allorder”>已卖出货品</a>  
  43.  <a href=”/lbt/release/release1″>发布供应产品</a><br />
  44.  <a href=”/lbt/sellercenter/allproduct?page=1&fenleiId=allproduct”>管理供应产品</a>   
  45.  <a href=”/lbt/news/toNewsReleaseListSeller?currentPage=1&newsStatus=2″>发布公告</a>
  46.  </dd>
  47.  </dl>
  48.  </@sec.authorize>

五、附加知识点:

页面标签的使用与权限配置相对应

authorize标签判断顺序是: access->url->ifNotGranted->ifAllGranted->ifAnyGranted

但他们的关系是“与”: 即只要其中任何一个属性不满足则该标签中间的内容将不会显示给用户,举个例子:

<sec:authorize  ifAllGranted=”ROLE_ADMIN,ROLE_MEMBER” ifNotGranted=”ROLE_SUPER”>满足才会显示给用户 </sec:authorize>

标签中间的内容只有在当前用户拥有ADMIN,MEMBER角色,但不拥有SUPER权限时才会显示!

access属性是基于角色判断,url属性是基于访问路径判断。

对于ifAllGranted ,ifNotGranted,ifAnyGranted属性的理解可以与集合api类比

Collection grantedAuths :当前用户拥有的权限
Collection requiredAuths : 当前要求的权限,即ifAllGranted ,ifNotGranted,ifAnyGranted 属性的值

满足ifAllGranted: 只需要grantedAuths.containsAll(requiredAuths);返回true即可
满足ifAnyGranted: 只需要grantedAuths.retainAll(requiredAuths);有内容即可(两集合有交集)
满足ifNotGranted:与Any相反,如果没有交集即可

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902