File tree Expand file tree Collapse file tree
spring-security-rest-basic-auth Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010
1111 <parent >
1212 <groupId >com.baeldung</groupId >
13- <artifactId >parent-spring-4 </artifactId >
13+ <artifactId >parent-spring-5 </artifactId >
1414 <version >0.0.1-SNAPSHOT</version >
15- <relativePath >../parent-spring-4 </relativePath >
15+ <relativePath >../parent-spring-5 </relativePath >
1616 </parent >
1717
1818 <dependencies >
2222 <dependency >
2323 <groupId >org.springframework.security</groupId >
2424 <artifactId >spring-security-web</artifactId >
25- <version >${org.springframework.security .version} </version >
25+ <version >${spring .version} </version >
2626 </dependency >
2727 <dependency >
2828 <groupId >org.springframework.security</groupId >
2929 <artifactId >spring-security-config</artifactId >
30- <version >${org.springframework.security .version} </version >
30+ <version >${spring .version} </version >
3131 </dependency >
3232
3333 <!-- Spring -->
9696 <dependency >
9797 <groupId >com.fasterxml.jackson.core</groupId >
9898 <artifactId >jackson-databind</artifactId >
99- <version >${jackson.version} </version >
99+ <version >${jackson-databind .version} </version >
100100 </dependency >
101101
102102 <!-- http -->
270270 </profiles >
271271
272272 <properties >
273- <!-- Spring -->
274- <org .springframework.security.version>4.2.6.RELEASE</org .springframework.security.version>
275273
276274 <!-- http -->
277275 <httpcore .version>4.4.5</httpcore .version>
280278 <!-- various -->
281279 <jstl .version>1.2</jstl .version>
282280 <javax .servlet.version>3.1.0</javax .servlet.version>
283- <jackson .version>2.8.5</jackson .version>
284281
285282 <!-- util -->
286283 <guava .version>19.0</guava .version>
Original file line number Diff line number Diff line change 22
33import org .baeldung .security .RestAuthenticationEntryPoint ;
44import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .context .annotation .Bean ;
56import org .springframework .context .annotation .Configuration ;
67import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
78import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
89import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
910import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
11+ import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
12+ import org .springframework .security .crypto .password .PasswordEncoder ;
1013import org .springframework .security .web .authentication .www .BasicAuthenticationFilter ;
1114
1215@ Configuration
@@ -20,7 +23,7 @@ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
2023 auth
2124 .inMemoryAuthentication ()
2225 .withUser ("user1" )
23- .password ("user1Pass" )
26+ .password (passwordEncoder (). encode ( "user1Pass" ) )
2427 .authorities ("ROLE_USER" );
2528 }
2629
@@ -38,4 +41,9 @@ protected void configure(HttpSecurity http) throws Exception {
3841
3942 http .addFilterAfter (new CustomFilter (), BasicAuthenticationFilter .class );
4043 }
44+
45+ @ Bean
46+ public PasswordEncoder passwordEncoder () {
47+ return new BCryptPasswordEncoder ();
48+ }
4149}
Original file line number Diff line number Diff line change 77import org .springframework .http .converter .HttpMessageConverter ;
88import org .springframework .http .converter .json .MappingJackson2HttpMessageConverter ;
99import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
10+ import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
1011import org .springframework .web .servlet .config .annotation .WebMvcConfigurerAdapter ;
1112
1213@ Configuration
1314@ EnableWebMvc
1415@ ComponentScan ("org.baeldung.web" )
15- public class WebConfig extends WebMvcConfigurerAdapter {
16+ public class WebConfig implements WebMvcConfigurer {
1617
1718 public WebConfig () {
1819 super ();
@@ -22,7 +23,6 @@ public WebConfig() {
2223
2324 @ Override
2425 public void configureMessageConverters (final List <HttpMessageConverter <?>> converters ) {
25- super .configureMessageConverters (converters );
2626 converters .add (new MappingJackson2HttpMessageConverter ());
2727 }
2828
Original file line number Diff line number Diff line change 22<beans : beans xmlns =" http://www.springframework.org/schema/security" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" xmlns : beans =" http://www.springframework.org/schema/beans"
33 xsi : schemaLocation ="
44 http://www.springframework.org/schema/security
5- http://www.springframework.org/schema/security/spring-security-4.2 .xsd
5+ http://www.springframework.org/schema/security/spring-security.xsd
66 http://www.springframework.org/schema/beans
7- http://www.springframework.org/schema/beans/spring-beans-4.3 .xsd"
7+ http://www.springframework.org/schema/beans/spring-beans.xsd"
88>
99
1010 <http create-session =" stateless" use-expressions =" true" >
1111
12- <http-basic />
12+ <http-basic entry-point-ref = " myBasicAuthenticationEntryPoint " />
1313
1414 </http >
1515
1616 <authentication-manager >
1717 <authentication-provider >
1818 <user-service >
19- <user name =" user1" password =" user1Pass" authorities =" ROLE_USER" />
19+ <user name =" user1" password =" {noop} user1Pass" authorities =" ROLE_USER" />
2020 </user-service >
2121 </authentication-provider >
2222 </authentication-manager >
2323
2424 <global-method-security pre-post-annotations =" enabled" />
25+
26+ <beans : bean id =" myBasicAuthenticationEntryPoint" class =" org.baeldung.basic.MyBasicAuthenticationEntryPoint" />
2527
2628</beans : beans >
Original file line number Diff line number Diff line change 11<?xml version =" 1.0" encoding =" UTF-8" ?>
22<beans xmlns =" http://www.springframework.org/schema/beans"
33 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
4- xsi : schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2 .xsd" >
4+ xsi : schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >
55
66</beans >
You can’t perform that action at this time.
0 commit comments