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安全框架的学习和原理解读
四、开始实战:
本文内容主要实现三部分内容:
- 在pom.xml文件中加入Security 坐标:
- <!--spring security 依赖包-->
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-core</artifactId>
- <version>3.2.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-web</artifactId>
- <version>3.2.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-config</artifactId>
- <version>3.2.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-taglibs</artifactId>
- <version>3.2.0.RELEASE</version>
- </dependency>
- 在web.xml中配置Security
- <!--配置Spring Security-->
- <!--filter的声明-->
- <filter>
- <filter-name>springSecurityFilterChain</filter-name>
- <filter-class>
- org.springframework.web.filter.DelegatingFilterProxy
- </filter-class>
- </filter>
- <!--mapping就是filter的映射,就是哪些文件用到这个filter-->
- <filter-mapping>
- <filter-name>springSecurityFilterChain</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- Controller文件代码 (SecurityConfig.java)
- @Configuration
- @EnableWebSecurity
- @Component
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- @Autowired
- private UserDetailService userDetailService;
- @Override
- protected void configure(HttpSecurity http) throws Exception{
- http.authorizeRequests()
- .antMatchers("/index/show").hasAnyRole("ADMIN","BUYER","SELLER")//个人首页只允许拥有ADMIN,BUYER,SELLER角色的用户访问
- .antMatchers("/cart/show").hasAnyRole("ADMIN","MAIJIA","SELLER")
- //在此后面可以根据自己的项目需要进行页面拦截的添加
- .anyRequest().authenticated()
- .and()
- .formLogin()
- .loginPage("/index/login").permitAll()//这里程序默认路径就是登陆页面,允许所有人进行登陆
- .loginProcessingUrl("/j_spring_security_check")//登陆提交的处理url
- .usernameParameter("j_username")//登陆用户名参数
- .passwordParameter("j_password")//登陆密码参数
- .failureUrl("/index/login?error=true")//登陆失败进行转发,这里回到登陆页面,参数error可以告知登陆状态
- .defaultSuccessUrl("/index/show")//登陆成功的url,这里去到个人首页
- .and().logout().logoutUrl("/j_spring_security_logout").permitAll().logoutSuccessUrl("/index/login?logout=true")//按顺序,第一个是登出的url,security会拦截这个url进行处理,所以登出不需要我们实现,第二个是登出url,logout告知登陆状态
- .and()
- .addFilter(myUsernamePasswordAuthenticationFilter)
- .rememberMe()
- .tokenValiditySeconds(604800)//记住我功能,cookies有限期是一周
- .and()
- .csrf().disable();
- }
- @Override
- public void configure(WebSecurity web) throws Exception{
- super.configure(web);
- }
- @Override
- public void configure(AuthenticationManagerBuilder auth) throws Exception{
- auth.userDetailsService(userDetailService);
- }
- @Override
- public AuthenticationManager authenticationManagerBean() throws Exception {
- return super.authenticationManagerBean();
- }
- }
- 接下来是Service层的代码(UserDetailService.java)
- @Service
- public class UserDetailService implements UserDetailsService {
- @Autowired
- private UserDetailsDao userDetailsDao;
- /**
- * 获取所属角色
- */
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- //查出用户名、密码、角色信息
- Users users = userDetailsDao.getUserByName(username);
- if (users==null) {
- throw new UsernameNotFoundException("找不到该账户信息!");
- }
- List<GrantedAuthority> list = new ArrayList<GrantedAuthority>(); //GrantedAuthority是security提供的权限类,
- list.add(new SimpleGrantedAuthority("ROLE_"+users.getRoles()));
- User auth_user = new User(users.getUsername(), users.getPassword(), list);
- return auth_user;
- }
- }
- 前端代码 .ftl文件(在此只粘出一部分代码,只显示一下用法)
在ftl文件头部加上,引入Security文件
<#assign sec=JspTaglibs["http://www.springframework.org/security/tags"]/>
- <#--没有登录时 能看到买家、卖家所有信息-->
- <@sec.authorize ifNotGranted="ROLE_ADMIN,ROLE_MAIJIA,ROLE_SELLER,ROLE_BOTHSM">
- <dl>
- <dt><a href="/jt/success/orderlist?page=1&orderPeriod=threemonth&orderType=allorder">买家中心</a></dt>
- <dd>
- <a href="/jt/success/orderlist?page=1&orderPeriod=threemonth&orderType=allorder">全部订单</a>
- <a href="#">优惠券</a> <br />
- <a href="/jt/cart/show" class="shoppingCart">我的购物车</a>
- <a href="/jt/favorite/myfavorites/1/time">我的收藏</a>
- </dd>
- </dl>
- <dl>
- <dt><a href="/jt/sellercenter/allproduct?page=1&fenleiId=allproduct">卖家中心</a> </dt>
- <dd>
- <a href="/jt/seller/soldprolist?page=1&orderPeriod=threemonth&orderType=allorder">已卖出货品</a>
- <a href="/jt/release/release1">发布供应产品</a><br />
- <a href="/jt/sellercenter/allproduct?page=1&fenleiId=allproduct">管理供应产品</a>
- <a href="/jt/news/toNewsReleaseListSeller?currentPage=1&newsStatus=2">发布公告</a>
- </dd>
- </dl>
- </@sec.authorize>
- <#--登陆后,买家中心 只有以买家身份登录可以看到-->
- <@sec.authorize ifAnyGranted="ROLE_MAIJIA">
- <dl>
- <dt><a href="/jt/success/orderlist?page=1&orderPeriod=threemonth&orderType=allorder">买家中心</a></dt>
- <dd>
- <a href="/jt/success/orderlist?page=1&orderPeriod=threemonth&orderType=allorder">全部订单</a>
- <a href="#">优惠券</a> <br />
- <a href="/jt/cart/show" class="shoppingCart">我的购物车</a>
- <a href="/jt/favorite/myfavorites/1/time">我的收藏</a>
- </dd>
- </dl>
- </@sec.authorize>
- <#--登陆后,卖家中心 只有以卖家身份登录可以看到-->
- <@sec.authorize ifAnyGranted="ROLE_SELLER">
- <dl>
- <dt><a href="/lbt/sellercenter/allproduct?page=1&fenleiId=allproduct">卖家中心</a> </dt>
- <dd>
- <a href="/lbt/seller/soldprolist?page=1&orderPeriod=threemonth&orderType=allorder">已卖出货品</a>
- <a href="/lbt/release/release1">发布供应产品</a><br />
- <a href="/lbt/sellercenter/allproduct?page=1&fenleiId=allproduct">管理供应产品</a>
- <a href="/lbt/news/toNewsReleaseListSeller?currentPage=1&newsStatus=2">发布公告</a>
- </dd>
- </dl>
- </@sec.authorize>