首先先讲一下什么是secrity。本人自己也是一知半解,看到了就记下来。
百度百科是这么说的:Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
Spring Security对Web安全性的支持大量地依赖于Servlet过滤器。
优点:人们使用Spring Security有很多种原因,不过通常吸引他们的是在J2EE Servlet规范或EJB规范中找不到典型企业应用场景的解决方案。
使用Spring Secrity 可以解决,他们不能在WAR 或 EAR 级别进行移植,需要对应用系统进行重新配置安全。除此之外还提供很多有用的,完全可以指定的其它安全特性。
其他的就不再 写了,想知道的可以去百度百科看看。
这个文件写的很好,可以参考这个搭建。其中页面控制器,我用了springboot自带的,加了web依赖,直接用就可以。其中的控制器改为了
package com.study.secrity.controller;
import com.study.secrity.entity.Msg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by lpw'ASUS on 2018/7/17.
*/
@Controller
@RequestMapping("/")
public class HomeController {
@RequestMapping("login")
public String loadLoginHtml(){
return "login";
}
@RequestMapping("index")
public String loadLoginRefHtml(){
System.out.print("ss");
return "index";
}
@RequestMapping("/")
public String index(Model model) {
Msg msg = new Msg("标题", "内容", "额外信息,只对管理员显示");
model.addAttribute("msg", msg);
System.out.print("走了----------");
return "index";
}
}
其中上面的文章中忽略了一个问题,就是在过滤的过程中静态文件也会被过滤,在未登陆授权的时候,是不能够加载静态文件的。在登陆后悔跳转dao到一个css文件中去。点击浏览器的返回键,会发现之前的登录页面有css样式了,再登陆就成功了。
解决办法贴代码。
package com.study.secrity.config;
import com.study.secrity.service.CustomUserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* Created by lpw'ASUS on 2018/7/17.
*/
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {//扩展SpringSecurity配置需要继承此类
@Bean
UserDetailsService customUserService(){//注册UserDetailsService的bean
return new CustomUserService();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserService()).passwordEncoder(new BCryptPasswordEncoder());//添加自定义的userDetailsService认证
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login","/bootstrap-3.3.7-dist/**").permitAll()//可以被直接加载的静态文件路径
.anyRequest().authenticated()//所有的请求需要认证即登陆后才能访问
.and()
.formLogin().loginPage("/login")
// .defaultSuccessUrl("/index")//指定成功后跳转的页面
.failureUrl("/login?error")//指定失败后爱转的页面
.permitAll() //登录页面可任意访问
.and()
.logout().permitAll();//注销请求可任意访问
}
}
还有就是我们想要配置讴歌角色可以访问个路径下的,
就是这行,但要注意的是数据库role表存的角色是ROLE_USER,这里填USER,它会识别 ROLE_ 后面的。表里面的角色,也要ROLE_ +角色名称。
在使用过程中发现,之前上面的mor默认的成功后tiao跳转的路径是失败的,没有走。经过查询,百度后发现,
那样写不行的下面这样,具体原因。
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests()
// 设置静态的资源允许所有访问
.antMatchers("/static/base/**").permitAll()
// 其他所有资源都需要登陆后才能访问
.anyRequest().authenticated()
// 设置默认登陆页面/login
.and().formLogin().loginPage("/login")
// 强制指定登陆成功后跳转的路劲
.successHandler(new ForwardAuthenticationSuccessHandler("/loginStatus?status=true"))
.failureUrl("/loginStatus?status=false")
.permitAll()
// 设置缓存,默认2周有效
.and().rememberMe().tokenValiditySeconds(1209600).key("mykey")
// 设置登出的路径和登出成功后访问的路径
.and().logout().logoutUrl("/loginOut").logoutSuccessUrl("/login").permitAll()
// 金庸crsf
.and().csrf().disable()
;
}