有个很大的疑问 ,这里login后为什么直接跳转到index 不是很明白 GetMapping(’/’)访问根目录是不是所有的目录都可以访问?
Spring Security 与Spring Boot集成
对build.gradle配置很重要
添加依赖:
//添加spring secuity 依赖 版本不需要自己指定
compile(‘org.springframework.boot:spring-boot-starter-security’)
//添加thymeleaf spring security依赖 这个版本需要自己指定
compile(‘org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE’)
创建了SecurityConfig 类
@EnableWebSecurity注解以及WebSecurityConfigurerAdapter一起配合提供基于web的security。
@Override
表明此方法是继承基类,子类进行了复写。
@Autowired
@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。当容器扫描到@Autowied、@Resource或@Inject时,就会在IoC容器自动查找需要的bean,并装配给该对象的属性.
//认证信息管理
Public void configureGlobal(){
}
//基于角色的权限管理
解决方案
Apache shrio
Spring security 功能就比较强大 社区也完善
********//显式访问控制 这是分配给相应的用户对应的权限就行 用户被允许就行,不是固定的允许 显式的权限控制:if (user.isPermitted(“projectReport:view:12345”)) { //show the project report button} else { //don’t show the button}
//隐式访问控制
,这种隐式的(静态字符串)形式的基于角色的访问控制方式难以满足需求。
这个是
代码块1. 隐式地基于角色的权限控制:
if (user.hasRole(“Project Manager”) ) {
//show the project report button
} else {
//don’t show the button
}
修改过的隐式的基于角色的权限控制:if (user.hasRole(“Project Manager”) || user.hasRole(“Department Manager”) ) {
//show the project report button
} else {
//don’t show the button
}
再来说一下今天使用spring security的代码总结
首先创建了config包里面放了securityconfig类
该类是安全配置类:
package com.spring.boot.bootstrap.config;
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注解以及WebSecurityConfigurerAdapter一起配合提供基于web的security
*/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 自定义配置
* 它允许对特定的http请求基于安全考虑进行配置。
*/
@Override
protected void configure(HttpSecurity http)throws Exception{
//http的权限请求
http.authorizeRequests()
// /css/**表示在/css文件下的所有文件都匹配也就是在css 文件夹下面的所有文件都有被访问的权限
.antMatchers("/css/**","/js/**","/fonts/**","/index").permitAll()//都可以访问
.antMatchers("/users/**").hasRole("ADMIN")//需要相应的角色才能访问 有这样的规则也就是符合"ADMIN"
.and()
.formLogin()//基于Form 表单登录验证
.loginPage("/login").failureUrl("/login-error");//自定义登录界面,登录的时间是在login,当发生错误时转至login-error
}
/**
* 认证信息管理
* AuthenticationManagerBuilder身份验证管理器
* inMemoryAuthentication()内存验证要有user 和password还有role也就是身份权限
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)throws Exception{
auth.inMemoryAuthentication().withUser("ljw").password("123").roles("ADMIN");
}
}
控制层:主要是和界面交互的
package com.spring.boot.bootstrap.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 主页控制器
*/
@Controller
public class MainController {
//访问根目录时重定向到index
@GetMapping("/")
public String root() {
return "redirect:/index";
}
//访问到Index时 进入index.html 其中会访问到sec:authorize="isAuthenticated()"
@GetMapping("/index")
public String index() {
return "index";
}
/**
* 获取登录界面
* @return
* 进入login后在login.html页面上有一个表单登录,用这个sec:authorize="isAuthenticated()"会进行判断是不是通过了身份验证
* 当判断通过会显示登录的用户名和一些信息,这里为什么会直接跳转到index应该是因为访问根目录getMapping('/')
* 然后就是是因为在安全配置类中有一个permitall()函数,配置的时候允许访问
* 访问/index
*/
@GetMapping("/login")
public String login() {
return "login";
}
@GetMapping("/login-error")
public String loginError(Model model) {
model.addAttribute("loginError", true);
model.addAttribute("errorMsg", "登陆失败,账号或者密码错误!");
return "login";
}
}
Login.html
<!DOCTYPE html>
<html xmlns:th = "http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head th:replace="~{fragments/header :: header}">
</head>
<body>
<div class="container blog-content-container"><!--container-->
<form th:action="@{~/login}" method="post">
<h2>请登录</h2>
<div class="form-group col-md-5">
<label for="username" class="col-form-label">账号</label>
<input type="text" class="form-control" id="username" name="username" maxlength="50">
</div>
<div class="form-group col-md-5">
<label for="password" class="col-form-label">密码</label>
<input type="password" class="form-control" id="password" name="password" maxlength="50">
</div>
<div class="form-group col-md-5">
<button type="submit" class="btn btn-primary">登录</button>
</div>
<div class="col-md-5" th:if="${loginError}">
<p class="blog-label-error" th:text="${errorMsg}"></p>
</div>
</form>
</div><!--container-->
<div th:replace="~{fragments/footer :: footer}"></div>
</body>
</html>