# spring-security-test **Repository Path**: dsjwangqing/spring-security-test ## Basic Information - **Project Name**: spring-security-test - **Description**: Spring Security学习小案例 - **Primary Language**: Java - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-04 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # spring-security-test #### 介绍 Spring Security学习小案例 #### 软件架构 软件架构说明 #### 安装教程 ## **==SpringSecurity入门案例==** ##### 1、在SpringBoot中直接引入Security依赖 ```yml org.springframework.boot spring-boot-starter-security ``` ##### 2、默认用户名下使用 1、创建SecurityController控制器 ```java @RestController public class SecurityController { @RequestMapping("/hello") public String sayHello(){ return "Hello bro!"; } } ``` 2、启动项目 ```bash 访问localhost:8080/hello, 会跳转到localhost:8080/logon 用户名为:user 密码为控制台打印的:Using generated security password:xxxx-xxxx-xxxx-xxxx 登录后跳转hello页面,显示:Hello bro! ``` ##### 3、application.yml中配置账户密码访问hello 1、 ```yml spring: security: user: name: admin password: admin ``` ##### 4、配置类设计账户密码访问hello ```java @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(AuthenticationManagerBuilder auth) throws Exception{ BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); String password = bCryptPasswordEncoder.encode("123456"); auth.inMemoryAuthentication() .withUser("user") .roles("user") .password(password); } @Bean PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } } ``` 登录账户:user ,密码:123456 配置类的账户密码全限高于application中的账户密码 ##### 5、忽略拦截,不登录即可访问 ```java @RestController public class SecurityController { /** * 不用登录就可访问 */ @RequestMapping("/hi") public String sayHi(){ return "Hi bro!"; } @RequestMapping("/hello") public String sayHello(){ return "Hello bro!"; } } ``` 配置类中重写 ```java @Override public void configure(WebSecurity web) throws Exception{ web.ignoring().antMatchers("/hi"); } ``` 访问localhost:8080/hi,会跳过登录直接返回Hi bro! #### 使用说明 1. xxxx 2. xxxx 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 码云特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)