[精讚] [會員登入]
287

spring boot 3 建立 Basic Authentication

參考以下網站, 建立一個http basic authentication 為例 https://www.geeksfo

分享此文連結 //n.sfs.tw/16266

分享連結 spring boot 3 建立 Basic Authentication@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2023-11-23 09:28:08 最後編修
2023-11-10 11:23:03 By igogo
 

 

 

 

參考以下網站, 建立一個http basic authentication 為例

https://www.geeksforgeeks.org/authentication-and-authorization-in-spring-boot-3-0-with-spring-security/

https://docs.spring.io/spring-security/reference/servlet/architecture.html#servlet-securityfilterchain

https://docs.spring.io/spring-security/reference/servlet/exploits/csrf.html

 

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

 

 

 

code

 

@SpringBootApplication
@RestController
public class BasicAuthApplication {

	public static void main(String[] args) {
		SpringApplication.run(BasicAuthApplication.class, args);
	}

	@RequestMapping("/")
	public String Index() {
		return "index page";
	}

	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

		http
				.authorizeHttpRequests((authorize) -> authorize
						.requestMatchers("/js/**").permitAll()
						.anyRequest().authenticated()
				)
				.csrf(c -> c.disable())
				.httpBasic(Customizer.withDefaults());


		return http.build();
	}

	@Bean
	public UserDetailsService userDetailsService() {
		User.UserBuilder users = User.withDefaultPasswordEncoder();
		UserDetails user =
				users
						.username("user")
                        .password("user")
						.roles("USER")
						.build();
		return new InMemoryUserDetailsManager(user);
	}

 

 

加入csrf

https://docs.spring.io/spring-security/reference/servlet/exploits/csrf.html#csrf-components

 

 

測試

 

@SpringBootTest
@AutoConfigureMockMvc
class BasicAuthApplicationTests {


	@Autowired
	private MockMvc mockMvc;


	@Test
	void shouldReturnDefaultMessage() throws Exception {

		MvcResult mvcResult = mockMvc
				.perform(get("/hello").with(httpBasic("user","user")))
				.andReturn();

		System.out.println(mvcResult.getResponse().getContentAsString());
	}


}

 

END

你可能感興趣的文章

spring security 使用MD5 hash 認證 spring security 預設使用BCrypt , 但是舊的系統使用md5 hash, @Bean Passwor

spring boot 3 建立 Basic Authentication 參考以下網站, 建立一個http basic authentication 為例 https://www.geeksfo

download a file from spring boot controllers ownload a file from spring boot controllers

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

download a file from spring boot controllers ownload a file from spring boot controllers

2018 hoc 頒獎 校慶到了,啦啦隊比賽如火如荼展開,學務主任將頒發獎狀給表現優異的班級。請完成以下程式碼,讓程式將啦啦隊表演成績由高至低依序輸出。

ArrayList 想移除特定值 想移出water, 使用lambda 的方式如下 List<String> fruits = new Arr

台中市網路應用競賽試場安排(一) 排試場就跟排班或是排課表一樣, 找不到一個都能讓所有人滿意的結果

[javascript] 將角色物件放到清單中,並依序讀出每個角色的X值 參考在scratch中建立三個角色並且給定值 http://n.sfs.tw/content/index/14716 一