赞
踩
- <!-- spring-security 安全框架依赖 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-security</artifactId>
- </dependency>
-
- <!-- spring-boot的web依赖(如果创建项目时没有勾选,可以手动加上) -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
springboot项目如果导入security依赖后会自动托管整个项目,前端在访问项目的任何路径时会被拦截并跳转到security默认的登录页面,登录用户名为user,密码为控制台启动项目时生成的随机密码
1.设置固定配置登录的用户(不常用)
- @Configuration
- //启动 权限管理功能
- @EnableGlobalMethodSecurity( prePostEnabled = true)
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- // 重写配置权限的方法
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- // 固定配置登录的用户
- auth.inMemoryAuthentication() // 内存验证
- .withUser("tom") //登录的用户名
- .password("{bcrypt}$2a$10$Wg.X1U3cKklWFqiZVmIzSeDynq3LcgXRlVhBIAW0s0tmZLRd.5QWy") 登录的mm
- .authorities("abc") // 设置权限字符
- .and()
- .withUser("tom2") //第二个用户,以此类推,.and()后可继续添加用户
- .password("{bcrypt}$2a$10$Wg.X1U3cKklWFqiZVmIzSeDynq3LcgXRlVhBIAW0s0tmZLRd.5QWy") //登录的密码
- .authorities("abc"); // 设置权限字符
- }
- }
2.从数据库查询认证,创建一个实现类,通过实现userDetailsService接口,重写里面的loadUserByUsername(String s)方法,当用户点击登录时,此方法的参数会将当前的用户名传入,我们可通过用户名从数据库查询用户信息,将查到的用户信息保存到UserDetails对象中并返回,security框架会将返回的UserDetails对象和当前登录的用户进行认证,如果相同则登录成功。(注意密码必须是带加密的),最后在配置类里设置通过userDetailsServiceI对象验证
- package com.security.springSecurity.service;
-
- import org.springframework.security.core.userdetails.User;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.core.userdetails.UserDetailsService;
- import org.springframework.security.core.userdetails.UsernameNotFoundException;
- import org.springframework.stereotype.Service;
-
- @Service
- public class UserDetailsServiceImpl implements UserDetailsService {
- @Override
- public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
- // 通过用户名去数据库查询密码及权限
- String username = s;
- String password = "{bcrypt}$2a$10$Wg.X1U3cKklWFqiZVmIzSeDynq3LcgXRlVhBIAW0s0tmZLRd.5QWy";//123456
- String authorities = "abc";
-
- // 将查询到的用户名、密码、权限保存到UserDetails中,框架会自动验证,如果前端传入的和UserDetails中保存一样则会登录成功
- UserDetails userDetails = User
- .builder()
- .username(username)
- .password(password)
- .authorities(authorities)
- .build();
- return userDetails;
- }
- }
- @Configuration
- //启动 权限管理功能
- @EnableGlobalMethodSecurity( prePostEnabled = true)
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- @Autowired
- private UserDetailsService userDetailsServiceImpl;
- // 重写配置权限的方法
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
-
- // 通过userDetailsServiceImpl对象验证
- auth.userDetailsService(userDetailsServiceImpl);
- }
- package com.security.springSecurity.config;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
- 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;
-
- @Configuration
- //启动 权限管理功能
- @EnableGlobalMethodSecurity( prePostEnabled = true)
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
-
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.authorizeRequests() // 对请求进行权限设置
- .antMatchers( // 设置匹配路径
- "/index.html",
- "/css/*",
- "/js/*",
- "/login.html",
- "/login"
- ).permitAll() // 对以上匹配路径全部放行
- .anyRequest().authenticated() // 对其他请求需要认证
- .and().formLogin() // 没有权限会自动跳到登录页
- .loginPage("/login.html") // 使用自己的登录页面
- .loginProcessingUrl("/login") // 设置登录时表单提交的路径
- .failureUrl("/login.html?error")// 登录失败的路径
- .defaultSuccessUrl("/index.html")// 设置登录成功的页面
- ;
- http.logout() // 设置登出
- .logoutUrl("/logout") // 设置登出的请求路径
- .logoutSuccessUrl("/login.html");//登出成功后返回的页面
-
- http.csrf().disable(); //关闭防跨越攻击,最好写上不然容易报错
- }
- }
- package com.security.springSecurity.controller;
-
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.security.core.annotation.AuthenticationPrincipal;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class TestController {
-
- //3.在请求路径的方法上加@PreAuthorize("hasAuthority('权限字符')")即可
- @RequestMapping("/s1")
- @PreAuthorize("hasAuthority('abc')")
- public String test01(){
- return "s1";
- }
-
- //4.在任何方法的参数上加UserDetails参数,并在参数前加@AuthenticationPrincipal注解
- // 此时在使用UserDetails参数就是当前登录时存入的用户
- @RequestMapping("/my")
- public String getMy(@AuthenticationPrincipal UserDetails userDetails){
- String username = userDetails.getUsername();
- return "当前用户用名是:"+username;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。