Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cd2ade4

Browse files
committed
Updated with Spring Security
1 parent e0ae08e commit cd2ade4

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

‎pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
<groupId>org.springframework.boot</groupId>
2828
<artifactId>spring-boot-starter-web</artifactId>
2929
</dependency>
30-
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-security</artifactId>
33+
</dependency>
3134
<dependency>
3235
<groupId>org.springframework.boot</groupId>
3336
<artifactId>spring-boot-devtools</artifactId>
@@ -44,6 +47,11 @@
4447
<artifactId>spring-boot-starter-test</artifactId>
4548
<scope>test</scope>
4649
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.security</groupId>
52+
<artifactId>spring-security-test</artifactId>
53+
<scope>test</scope>
54+
</dependency>
4755
</dependencies>
4856

4957
<build>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.reuben.store.config;
2+
3+
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
8+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
11+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
12+
import org.springframework.security.crypto.password.PasswordEncoder;
13+
14+
@Configuration
15+
@EnableWebSecurity
16+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
17+
18+
@Autowired //Could not autowire. No beans of 'PasswordEncoder' type found.
19+
private PasswordEncoder passwordEncoder;
20+
21+
@Override
22+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
23+
auth.inMemoryAuthentication()
24+
.passwordEncoder(passwordEncoder)
25+
.withUser("reuben").password(passwordEncoder.encode("1234")).authorities("ADMIN")
26+
.and()
27+
.withUser("rhea").password(passwordEncoder.encode("rhea")).authorities("USER");
28+
}
29+
30+
@Override
31+
protected void configure(HttpSecurity http) throws Exception {
32+
http.authorizeRequests()
33+
.antMatchers("/api/public/test").permitAll()
34+
.antMatchers("/api/private/user/test").authenticated()
35+
.antMatchers("/api/private/admin/test").hasAuthority("ADMIN")// hasAnyAuthority("ADMIN","USER")
36+
.and()
37+
.httpBasic();
38+
http.csrf().disable();
39+
http.cors().disable();
40+
}
41+
42+
43+
@Bean
44+
public PasswordEncoder getPasswordEncoder() {
45+
return new BCryptPasswordEncoder();
46+
}
47+
}

‎src/main/java/com/reuben/store/controller/MainController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public Product AddProduct(@PathVariable("vendor_id") Long vendor_id,
8181
}
8282

8383
@PostMapping("/customer/product/{customer_id}/{product_id}")
84-
public ResponseEntity<?> enrollStudentInCourse(
84+
public ResponseEntity<?> customerPurchaseProduct(
8585
@PathVariable("customer_id") Long customer_id,
8686
@PathVariable("product_id") Long product_id
8787
){

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /