| |
| <properties> |
| <mybatis-plus.version>3.2.0</mybatis-plus.version> |
| <druid.version>1.1.12</druid.version> |
| <oauth2-autoconfigure.version>2.1.3.RELEASE</oauth2-autoconfigure.version> |
| <kaptcha.version>2.3.2</kaptcha.version> |
| <fastjson.version>1.2.8</fastjson.version> |
| <commons-lang.version>2.6</commons-lang.version> |
| <commons-collections.version>3.2.2</commons-collections.version> |
| <commons-io.version>2.6</commons-io.version> |
| </properties> |
| |
| |
| <dependencyManagement> |
| <dependencies> |
| |
| <dependency> |
| <groupId>com.baomidou</groupId> |
| <artifactId>mybatis-plus-boot-starter</artifactId> |
| <version>${mybatis-plus.version}</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.alibaba</groupId> |
| <artifactId>druid</artifactId> |
| <version>${druid.version}</version> |
| </dependency> |
| |
| |
| <dependency> |
| <groupId>org.springframework.security.oauth.boot</groupId> |
| <artifactId>spring-security-oauth2-autoconfigure</artifactId> |
| <version>${oauth2-autoconfigure.version}</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.github.penggle</groupId> |
| <artifactId>kaptcha</artifactId> |
| <version>${kaptcha.version}</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.alibaba</groupId> |
| <artifactId>fastjson</artifactId> |
| <version>${fastjson.version}</version> |
| </dependency> |
| <dependency> |
| <groupId>commons-lang</groupId> |
| <artifactId>commons-lang</artifactId> |
| <version>${commons-lang.version}</version> |
| </dependency> |
| <dependency> |
| <groupId>commons-collections</groupId> |
| <artifactId>commons-collections</artifactId> |
| <version>${commons-collections.version}</version> |
| </dependency> |
| <dependency> |
| <groupId>commons-io</groupId> |
| <artifactId>commons-io</artifactId> |
| <version>${commons-io.version}</version> |
| </dependency> |
| </dependencies> |
| </dependencyManagement> |
| |
| <build> |
| <plugins> |
| <plugin> |
| <groupId>org.apache.maven.plugins</groupId> |
| <artifactId>maven-compiler-plugin</artifactId> |
| <version>3.7.0</version> |
| <configuration> |
| <source>1.8</source> |
| <target>1.8</target> |
| <encoding>UTF-8</encoding> |
| </configuration> |
| </plugin> |
| </plugins> |
| </build> |
| <dependencies> |
| |
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.alibaba</groupId> |
| <artifactId>fastjson</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>commons-lang</groupId> |
| <artifactId>commons-lang</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>commons-collections</groupId> |
| <artifactId>commons-collections</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>commons-io</groupId> |
| <artifactId>commons-io</artifactId> |
| </dependency> |
| </dependencies> |
- resources文件夹下编写logback.xml
| <?xml version="1.0" encoding="UTF-8"?> |
| <configuration> |
| |
| |
| <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> |
| <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> |
| <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> |
| |
| <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> |
| |
| <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> |
| <layout class="ch.qos.logback.classic.PatternLayout"> |
| <pattern>${CONSOLE_LOG_PATTERN}</pattern> |
| </layout> |
| </appender> |
| <root level="info"> |
| <appender-ref ref="stdout" /> |
| </root> |
| </configuration> |
| <dependencies> |
| |
| <dependency> |
| <groupId>com.ychen.security</groupId> |
| <version>1.0-SNAPSHOT</version> |
| <artifactId>base</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-security</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>servlet-api</artifactId> |
| <version>2.5</version> |
| </dependency> |
| </dependencies> |
| @Configuration |
| @EnableWebSecurity |
| public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { |
| |
| Logger logger = LoggerFactory.getLogger(getClass()); |
| |
| @Bean |
| public PasswordEncoder passwordEncoder() { |
| |
| return new BCryptPasswordEncoder(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| @Override |
| protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| |
| String password = passwordEncoder().encode("admin"); |
| logger.info("加密之后存储的密码:" + password); |
| auth.inMemoryAuthentication().withUser("admin") |
| .password(password).authorities("ADMIN"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| @Override |
| protected void configure(HttpSecurity http) throws Exception { |
| http.formLogin() |
| |
| .loginProcessingUrl("/login") |
| |
| .defaultSuccessUrl("/success").permitAll() |
| |
| .and().authorizeRequests() |
| .antMatchers("/test").permitAll() |
| |
| .anyRequest().authenticated() |
| |
| .and().csrf().disable(); |
| } |
| |
| } |
| <dependencies> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-web</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.ychen.security</groupId> |
| <version>1.0-SNAPSHOT</version> |
| <artifactId>core</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-configuration-processor</artifactId> |
| <optional>true</optional> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-test</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-devtools</artifactId> |
| </dependency> |
| </dependencies> |
| @SpringBootApplication |
| public class WebApplication { |
| |
| public static void main(String[] args) { |
| SpringApplication.run(WebApplication.class, args); |
| } |
| |
| } |
| @Controller |
| public class TestController { |
| |
| |
| @RequestMapping({"/index", "/", ""}) |
| @ResponseBody |
| public String index() { |
| return "index"; |
| } |
| |
| |
| @RequestMapping("/test") |
| @ResponseBody |
| public String test() { |
| return "test"; |
| } |
| |
| |
| @GetMapping("/success") |
| @ResponseBody |
| public String success(){ |
| return "success"; |
| } |
| |
| } |
-
未认证时,直接访问test接口,访问成功

-
未认证时,直接访问index接口,跳转到认证页面

-
输入用户名和密码,跳转到index

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术