最近在跑demo的過程當中,訪問swagger頁面的時候須要驗證登陸,記得在以前寫的代碼中是關閉了security驗證,無需登陸成功訪問,直接在application.yml配置文件中添加上:java
management.security.enabled: falsespring
發現報錯,其實在添加的過程當中就發現 sql
此配置已經失效 ,經查閱發現spring boot 2.0+以後這樣配置就不能生效了數據庫
可是咱們能夠在代碼中去配置。
咱們能夠新建一個類SecurityConfig 繼承WebSecurityConfigurerAdapter類,
而後重寫父類中的configure(HttpSecurity http) 方法。
idea中重寫方法的快捷鍵默認是crtl+o
springboot
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().permitAll().and().logout().permitAll();//配置不須要登陸驗證 } }
如今再來訪問任意的接口,不須要再驗證啦。app
此外,再補充一點,security默認的username=user,password是咱們運行程序的時候生成的ide
感謝:https://blog.csdn.net/c851204293/article/details/92975654this
1. 建立基本spring boot項目url
2. 加入h2數據庫依賴idea
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>test</scope>
</dependency>
3. 配置配置文件
spring.jpa.database=h2
spring.jpa.show-sql=truespring.datasource.driver-class-name=org.h2.Driver
# 內存模式
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=# 只進行表的更新。默認是刪除原來的表結構從新生成 這樣會出問題。
spring.jpa.hibernate.ddl-auto = updatespring.h2.console.path=/h2-console
spring.h2.console.enabled=true#其中有三個值,always爲始終執行初始化,embedded只初始化內存數據庫(默認值),如h2等,never爲不執行初始化。
spring.datasource.initialization-mode=embedded
配置文件內容,和h2數據庫登陸信息一致
4.代碼結構
entity
import java.util.Collection; import java.util.Date; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import lombok.Data; @Entity @Table(name = "user") @Data public class User { @Id @Column(columnDefinition = "int") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String password; private String email; @Column(name = "phone_number") //映射數據庫中的字段名,與之對應。屬性用駝峯命名 private String phoneNumber; private int status; @Column(name = "create_time") private Date createTime; @Column(name = "last_login_time") private Date lastLoginTime; @Column(name = "last_update_time") private Date lastUpdateTime; private String avatar; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getLastLoginTime() { return lastLoginTime; } public void setLastLoginTime(Date lastLoginTime) { this.lastLoginTime = lastLoginTime; } public Date getLastUpdateTime() { return lastUpdateTime; } public void setLastUpdateTime(Date lastUpdateTime) { this.lastUpdateTime = lastUpdateTime; } public String getAvatar() { return avatar; } public void setAvatar(String avatar) { this.avatar = avatar; } }
dao層
public interface UserRepository extends JpaRepository<User, Long> { List<User> getUsersByName(String Name); }
controller層
@RestController public class ControllerTest { @Autowired private UserRepository userRepository; @GetMapping("userData") public void userData(){ List<User> list = userRepository.findAll(); System.out.println("user數據爲:" + list.toString()); } @GetMapping("user") public void usera(){ Optional<User> user = userRepository.findById(1L); System.out.println("user數據爲:" + user.get().getName()); } }
5.訪問
http://localhost:8080/userData
登陸h2控制檯