Spring Security
和 Apache Shiro
都是安全框架,为Java应用程序提供身份认证和受权。css
重
量级安全框架轻
量级安全框架关于shiro的权限认证与受权可参考小编的另一篇文章 : SpringBoot集成Shiro 实现动态加载权限java
https://blog.csdn.net/qq_38225558/article/details/101616759git
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
@RestController public class IndexController { @GetMapping("/index") public String index() { return "Hello World ~"; } }
舒适小提示:在不进行任何配置的状况下,Spring Security 给出的默认用户名为user
密码则是项目在启动运行时随机生成的一串字符串,会打印在控制台,以下图:
当咱们访问index首页的时候,系统会默认跳转到login页面进行登陆认证web
认证成功以后才会跳转到咱们的index页面spring
除了上面Spring Security在不进行任何配置下默认给出的用户user
密码随项目启动生成随机字符串,咱们还能够经过如下方式配置数据库
spring: security: user: name: admin # 用户名 password: 123456 # 密码
新建Security 核心配置类继承WebSecurityConfigurerAdapter
json
@Configuration @EnableWebSecurity // 启用Spring Security的Web安全支持 public class SecurityConfig extends WebSecurityConfigurerAdapter { /** * 将用户设置在内存中 * @param auth * @throws Exception */ @Autowired public void config(AuthenticationManagerBuilder auth) throws Exception { // 在内存中配置用户,配置多个用户调用`and()`方法 auth.inMemoryAuthentication() .passwordEncoder(passwordEncoder()) // 指定加密方式 .withUser("admin").password(passwordEncoder().encode("123456")).roles("ADMIN") .and() .withUser("test").password(passwordEncoder().encode("123456")).roles("USER"); } @Bean public PasswordEncoder passwordEncoder() { // BCryptPasswordEncoder:Spring Security 提供的加密工具,可快速实现加密加盐 return new BCryptPasswordEncoder(); } }
这种方式也就是咱们项目中一般使用的方式,这个留到后面的文章再说跨域
相关代码都有注释相信很容易理解安全
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { /** * 登陆处理 * @param http * @throws Exception */ @Override protected void configure(HttpSecurity http) throws Exception { // 开启登陆配置 http.authorizeRequests() // 标识访问 `/index` 这个接口,须要具有`ADMIN`角色 .antMatchers("/index").hasRole("ADMIN") // 容许匿名的url - 可理解为放行接口 - 多个接口使用,分割 .antMatchers("/", "/home").permitAll() // 其他全部请求都须要认证 .anyRequest().authenticated() .and() // 设置登陆认证页面 .formLogin().loginPage("/login") // 登陆成功后的处理接口 - 方式① .loginProcessingUrl("/home") // 自定义登录用户名和密码属性名,默认为 username和password .usernameParameter("username") .passwordParameter("password") // 登陆成功后的处理器 - 方式② // .successHandler((req, resp, authentication) -> { // resp.setContentType("application/json;charset=utf-8"); // PrintWriter out = resp.getWriter(); // out.write("登陆成功..."); // out.flush(); // }) // 配置登陆失败的回调 .failureHandler((req, resp, exception) -> { resp.setContentType("application/json;charset=utf-8"); PrintWriter out = resp.getWriter(); out.write("登陆失败..."); out.flush(); }) .permitAll()//和表单登陆相关的接口通通都直接经过 .and() .logout().logoutUrl("/logout") // 配置注销成功的回调 .logoutSuccessHandler((req, resp, authentication) -> { resp.setContentType("application/json;charset=utf-8"); PrintWriter out = resp.getWriter(); out.write("注销成功..."); out.flush(); }) .permitAll() .and() .httpBasic() .and() // 关闭CSRF跨域 .csrf().disable(); } /** * 忽略拦截 * @param web * @throws Exception */ @Override public void configure(WebSecurity web) throws Exception { // 设置拦截忽略url - 会直接过滤该url - 将不会通过Spring Security过滤器链 web.ignoring().antMatchers("/getUserInfo"); // 设置拦截忽略文件夹,能够对静态资源放行 web.ignoring().antMatchers("/css/**", "/js/**"); } }
WebSecurityConfigurerAdapter