Spring Security

I wanna Config The Spring Config class but due to changemnet in version i am not able to do it . Can anyone Help
package com.ShelfSpace.ShelfSpace.Config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfiguration{

public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {

httpSecurity.authorizeHttpRequests(x->{
x.requestMatchers("/").permitAll();
});

return httpSecurity.build();
}
}
package com.ShelfSpace.ShelfSpace.Config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfiguration{

public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {

httpSecurity.authorizeHttpRequests(x->{
x.requestMatchers("/").permitAll();
});

return httpSecurity.build();
}
}
i want to make a custom login page and authenticate every endpoint excpet the / this one how could i achieve it ?
32 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @Danix! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st7mo ago
What happens with your current config? How is that different from what you expect?
Danix
DanixOP7mo ago
i am doing it like this but stil not able to access the /registerPage i dont know why ?
dan1st
dan1st7mo ago
What happens when attempting to access it?
Danix
DanixOP7mo ago
@Bean
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/").permitAll();
auth.requestMatchers("/registerPage").permitAll();
auth.anyRequest().authenticated();
})
.formLogin(login -> login
.loginPage("/LoginPage")
.loginProcessingUrl("/LoginPage")
.defaultSuccessUrl("/")
.permitAll())
.logout(logout -> logout
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.permitAll());

return httpSecurity.build();
}
@Bean
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/").permitAll();
auth.requestMatchers("/registerPage").permitAll();
auth.anyRequest().authenticated();
})
.formLogin(login -> login
.loginPage("/LoginPage")
.loginProcessingUrl("/LoginPage")
.defaultSuccessUrl("/")
.permitAll())
.logout(logout -> logout
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.permitAll());

return httpSecurity.build();
}
returning to /LoginPage Everytime
dan1st
dan1st7mo ago
auth.anyRequest().authenticated() that means any request that doesn't have a specific configuration before that requires authentication You can put the
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/").permitAll();
auth.requestMatchers("/registerPage").permitAll();
auth.anyRequest().authenticated();
})
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/").permitAll();
auth.requestMatchers("/registerPage").permitAll();
auth.anyRequest().authenticated();
})
below
Danix
DanixOP7mo ago
but i have permitted the /registerPage as well right ?
dan1st
dan1st7mo ago
Is /registerPage redirected anywhere? I think that one should be permitted
Danix
DanixOP7mo ago
Danix
DanixOP7mo ago
package com.ShelfSpace.ShelfSpace.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ShelfBookController {

@GetMapping("/")
public String homePage() {
return "home";
}

@GetMapping("/LoginPage")
public String loginPage() {
return "login";
}

@GetMapping("/registerPage")
public String registerPage() {
return "registration";
}


}
package com.ShelfSpace.ShelfSpace.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ShelfBookController {

@GetMapping("/")
public String homePage() {
return "home";
}

@GetMapping("/LoginPage")
public String loginPage() {
return "login";
}

@GetMapping("/registerPage")
public String registerPage() {
return "registration";
}


}
now i have changed everything but still not reachable ->
<form action="/processRegister" th:action="@{/processRegister}" method="post">
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="name" th:field="*{name}" required>
</div>

<div>
<label for="email">Email:</label>
<input type="email" id="email" name="email" th:field="*{email}" required>
</div>

<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password" th:field="*{password}" required>
</div>

<button type="submit">Register</button>
</form>
<form action="/processRegister" th:action="@{/processRegister}" method="post">
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="name" th:field="*{name}" required>
</div>

<div>
<label for="email">Email:</label>
<input type="email" id="email" name="email" th:field="*{email}" required>
</div>

<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password" th:field="*{password}" required>
</div>

<button type="submit">Register</button>
</form>
<div class="container register-container">
<h2 class="text-center">Login</h2>
<form th:action="@{/LoginPage}" method="post" class="register-form">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email" required>
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-success btn-block">Submit</button>
</form>
<p class="text-center">Don't have an account? <a th:href="@{/registerPage}">Register</a></p>
</div>
<div class="container register-container">
<h2 class="text-center">Login</h2>
<form th:action="@{/LoginPage}" method="post" class="register-form">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email" required>
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-success btn-block">Submit</button>
</form>
<p class="text-center">Don't have an account? <a th:href="@{/registerPage}">Register</a></p>
</div>
@GetMapping("/registerPage")
public String registerPage() {
return "registration";
}

@PostMapping("/processRegister")
public String processRegister() {
return "hii";
}
@GetMapping("/registerPage")
public String registerPage() {
return "registration";
}

@PostMapping("/processRegister")
public String processRegister() {
return "hii";
}
package com.ShelfSpace.ShelfSpace.Config;

@Configuration
public class SecurityConfiguration {

@Bean
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/" , "/processRegister").permitAll();
auth.requestMatchers("/registerPage").permitAll();
auth.anyRequest().authenticated();
})
.formLogin(login -> login
.loginPage("/LoginPage")
.loginProcessingUrl("/LoginPage")
.defaultSuccessUrl("/")
.permitAll())
.logout(logout -> logout
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.permitAll());

return httpSecurity.build();
}

@Bean
UserDetailsService userDetailsService() {
UserDetails user = org.springframework.security.core.userdetails.User.withUsername("deepak")
.password(encoder().encode("deepak"))
.roles("USER")
.build();

return new InMemoryUserDetailsManager(user);
}

@Bean
PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
package com.ShelfSpace.ShelfSpace.Config;

@Configuration
public class SecurityConfiguration {

@Bean
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/" , "/processRegister").permitAll();
auth.requestMatchers("/registerPage").permitAll();
auth.anyRequest().authenticated();
})
.formLogin(login -> login
.loginPage("/LoginPage")
.loginProcessingUrl("/LoginPage")
.defaultSuccessUrl("/")
.permitAll())
.logout(logout -> logout
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.permitAll());

return httpSecurity.build();
}

@Bean
UserDetailsService userDetailsService() {
UserDetails user = org.springframework.security.core.userdetails.User.withUsername("deepak")
.password(encoder().encode("deepak"))
.roles("USER")
.build();

return new InMemoryUserDetailsManager(user);
}

@Bean
PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
@dan1st | Daniel
dan1st
dan1st7mo ago
What happens which you don't expect to happen?
Danix
DanixOP7mo ago
i want the registe Page but when i click on register through login page then it still redirect me into loginpage endpoint @dan1st | Daniel
dan1st
dan1st7mo ago
try replacing
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/" , "/processRegister").permitAll();
auth.requestMatchers("/registerPage").permitAll();
auth.anyRequest().authenticated();
})
.authorizeHttpRequests(auth -> {
auth.requestMatchers("/" , "/processRegister").permitAll();
auth.requestMatchers("/registerPage").permitAll();
auth.anyRequest().authenticated();
})
with
.authorizeHttpRequests(auth ->
auth.requestMatchers("/" , "/processRegister").permitAll()
.requestMatchers("/registerPage").permitAll()
.anyRequest().authenticated()
)
.authorizeHttpRequests(auth ->
auth.requestMatchers("/" , "/processRegister").permitAll()
.requestMatchers("/registerPage").permitAll()
.anyRequest().authenticated()
)
Danix
DanixOP7mo ago
still getting same
dan1st
dan1st7mo ago
When exactly are you redirected? When loading the register page or after sending username/password?
Danix
DanixOP7mo ago
i am not sending the username and password right now bcz i am just checking the endpoints and if the user is not registred then how it could register itseld first .
dan1st
dan1st7mo ago
Are you redirected on the GET endpoint? Can you show the main class including the package declaration?
Danix
DanixOP7mo ago
i am getting 302 with /registerpage endpoint means Get one when i click on register
package com.ShelfSpace.ShelfSpace;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
public class ShelfSpaceApplication {

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

}
package com.ShelfSpace.ShelfSpace;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
public class ShelfSpaceApplication {

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

}
dan1st
dan1st7mo ago
/registerpage? Not /registerPage?
Danix
DanixOP7mo ago
package com.ShelfSpace.ShelfSpace.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class ShelfBookController {

@GetMapping("/")
public String homePage() {
return "home";
}

@GetMapping("/LoginPage")
public String loginPage() {
return "login";
}

@GetMapping("/registerPage")
public String registerPage() {
return "registration";
}

@PostMapping("/processRegister")
public String processRegister() {
return "hii";
}


}
package com.ShelfSpace.ShelfSpace.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class ShelfBookController {

@GetMapping("/")
public String homePage() {
return "home";
}

@GetMapping("/LoginPage")
public String loginPage() {
return "login";
}

@GetMapping("/registerPage")
public String registerPage() {
return "registration";
}

@PostMapping("/processRegister")
public String processRegister() {
return "hii";
}


}
dan1st
dan1st7mo ago
What are you requesting in your browser? What's the link in the login page?
Danix
DanixOP7mo ago
No description
dan1st
dan1st7mo ago
Can you enable Spring Security debug logging?
Danix
DanixOP7mo ago
No description
dan1st
dan1st7mo ago
You did restart the application after making the changes, right?
Danix
DanixOP7mo ago
Yehh I do
dan1st
dan1st7mo ago
.
Danix
DanixOP7mo ago
Yehh ok after opening of debugg and clicking on register i got a huge error
Danix
DanixOP7mo ago
dan1st
dan1st7mo ago
check registration.html line 104, col 54 It doesn't know name
Danix
DanixOP7mo ago
ok got it
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?