使用和配置spring security,一个基本的权限管理模块
引入这两个包,版本自选,目前是2.5
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${springSecuroty.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${springSecuroty.version}</version> </dependency>
新建一个class,目的是配置启动web app时加载spring security
因为是简单配置,所以没有重写里面的任何方法,实际项目中可以根据需求重写里面的方法
package com.demo.config.spring; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; @Order(Ordered.HIGHEST_PRECEDENCE) public class SecurityInit extends AbstractSecurityWebApplicationInitializer { }
具体的spring security权限配置
用户名和密码读取数据库进行鉴权的配置
如下:
package com.demo.config.spring; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private DataSource dataSourceMySQL; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .jdbcAuthentication() .dataSource(dataSourceMySQL) .usersByUsernameQuery(this.getUserQuery()) .authoritiesByUsernameQuery(this.getAuthoritiesQuery()); } @Override protected void configure(HttpSecurity http) throws Exception { http .csrf() //防止csrf .disable() .authorizeRequests() //css js的目录设为不需要验证 .antMatchers("/resources/**") .permitAll() .anyRequest() //所有请求都要经过验证 .authenticated() .and() .formLogin() //登陆页面设为不需要验证 .loginPage("/login") .permitAll() .and() .logout() //登出请求设为不需要验证 .permitAll() .and() .headers() //如果使用iframe的话需要这段配置 .frameOptions() .sameOrigin() .httpStrictTransportSecurity() .disable(); } private String getUserQuery() { return "SELECT t.user_name as 'username', t.pass_word as 'password', t.enabled as 'enabled' FROM demo.users t WHERE t.user_name = ?"; } private String getAuthoritiesQuery() { return "SELECT t.user_name as 'username', t.user_auth as 'authority' FROM demo.authorities t WHERE t.user_name = ?"; } }
数据库里面有两张表
user表,字段有username,password,enable
authority表,权限等级表,字段有id,username,authority
另外,如果想要根据自己的需求定制登陆登出
新建一个这样的controller:
import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; @RestController public class AuthenticationController { @RequestMapping(value = "/login", produces = "text/html; charset=utf-8") public ModelAndView login(HttpServletRequest request){ return new ModelAndView("loginPage"); } }
以及一个个性化的登录页面,处理所有的登录和登出:
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Login</title> </head> <body> <form name="f" action="login" method="post"> <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /> <fieldset> <c:if test="${param.error != null}"> <p><span style="color:red;">Invalid username and password.</span></p> </c:if> <c:if test="${param.logout != null}"> <p><span style="color:red;">You have been logged out.</span></p> </c:if> <legend>Please Login</legend> <label for="username">Username</label> <input type="text" id="username" name="username" /> <label for="password">Password</label> <input type="password" id="password" name="password" /> <div class="form-actions"> <button type="submit" class="btn">Log in</button> </div> </fieldset> </form> </body> </html>
补充:
1. 登陆自动转发请求/login,可以参考前面SecurityConfig.class的代码进行修改。
2. 如果输错密码,会自动转发请求/login?error
3. 登出自动转发请求/login?logout
个性化的页面,加密方式,等等都可以按需定制,spring security很强大的。
配置完成,网站有了全局的权限管理咯
以上
相关推荐
spring cloud2.0 eureka server spring security配置,与spring cloud1.X还是有很大区别
spring security配置实例
Spring Security 配置实例XML文件
spring security 配置(含源码)和教程
SpringSecurity配置类
NULL 博文链接:https://357029540.iteye.com/blog/2329730
我们在使用SpringSecurity作为后台权限框架的时候,框架给我们提供了配置登录请求的接口,供我们配置登录链接,当我们配置了登录链接地址后,前端访问登陆请求的时候显示无权限。 异常分析 由于SpringSecurity的...
springsecurity使用配置详解,压缩包里包含主要的代码和详细的word文件说明。
主要介绍了详解spring security 配置多个AuthenticationProvider ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
介绍了Spring Security 的配置方法及其使用技巧
spring-security多登录页面配置,包括前台和后台分开登录界面,注销登录返回不同的界面等。
NULL 博文链接:https://lgd-lc.iteye.com/blog/873765
spring security配置项目下载,里面前台使用了easyui。 我的项目是用maven搭建的,如果你配置了maven,那么就会可以很轻松的运行起项目来了, 步骤: 1、在我的项目下找到database文件夹,把里面的union_ssh.sql文件...
之前的版本里面没有带数据库建表语句,该版本完整代码(包含所需jar包)+注释+教程+sql代码,元芳说这段代码绝对可以运行。也可按照附带的教程一步一个脚印,包学包会,亲测。
NULL 博文链接:https://lihao312.iteye.com/blog/1909205
NULL 博文链接:https://whp0731.iteye.com/blog/453796
Spring Security 2 配置精讲
Spring Security 3.1.3配置实例
NULL 博文链接:https://zjy7554.iteye.com/blog/2021785
spring_security 配置