Spring Boot 自定义 config

java -jar demo.jar --spring.config.name=foo
读 foo.properties

最好是放到 /config 下(相对jar包) 这样优先级最高

这个是全量的,原来的 application.properties 就失效了

但是 如果用
java -jar demo.jar --spring.profiles.active=foo

这样会覆盖,相对于的配置文件名是application-foo.properties

个人感觉还是全量覆盖好,因为方法2,一旦写错了key的名字,还是会读老的,可能就没那么容易被发现。

方法一一旦写错了key的名字,就注入不进去了,在启动的时候就会报错,这样也会让把需要注入的值才写上去,肯定不变的还是老老实实用常量类吧

reference

  1. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
以下是Spring Boot整合Spring Security自定义登录的步骤: 1. 引入依赖:在pom.xml文件中引入Spring Security和Thymeleaf的依赖。 2. 配置Spring Security:在Spring Boot的配置类中添加@EnableWebSecurity注解,并继承WebSecurityConfigurerAdapter类,重写configure(HttpSecurity http)方法,配置登录页面、登录请求、登录成功后的跳转等信息。 3. 自定义登录页面:在resources/templates目录下创建login.html文件,使用Thymeleaf模板引擎实现自定义登录页面。 4. 自定义登录表单:在login.html文件中添加表单,表单中包含用户名和密码两个输入框。 5. 自定义登录请求:在Spring Boot的控制器类中添加/login请求的处理方法,该方法接收用户名和密码参数,并使用Spring Security提供的AuthenticationManager进行认证。 6. 自定义认证逻辑:在Spring Security的配置类中重写configure(AuthenticationManagerBuilder auth)方法,实现自定义的认证逻辑。 7. 自定义登录成功后的跳转:在Spring Security的配置类中重写configure(HttpSecurity http)方法,配置登录成功后的跳转页面。 下面是示例代码: 1. 引入依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> ``` 2. 配置Spring Security: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login", "/register").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .permitAll() .and() .logout() .permitAll(); } } ``` 3. 自定义登录页面: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login</h1> <form th:action="@{/login}" method="post"> <div> <label for="username">Username:</label> <input type="text" id="username" name="username" required autofocus> </div> <div> <label for="password">Password:</label> <input type="password" id="password" name="password" required> </div> <button type="submit">Login</button> </form> </body> </html> ``` 4. 自定义登录表单:在login.html文件中添加表单,表单中包含用户名和密码两个输入框。 5. 自定义登录请求: ```java @Controller public class LoginController { @GetMapping("/login") public String login() { return "login"; } @PostMapping("/login") public String doLogin(@RequestParam String username, @RequestParam String password) { // 使用AuthenticationManager进行认证 return "redirect:/home"; } } ``` 6. 自定义认证逻辑: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } } ``` 7. 自定义登录成功后的跳转: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login", "/register").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .permitAll() .and() .logout() .permitAll(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值