Spring Security

I dont know why but i am not getting default login page after enabling the @WebSecurity annoation ..
package secuityConfig;

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

}
package secuityConfig;

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

}
package secuityConfig;

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

public class SecurityIntializer extends AbstractSecurityWebApplicationInitializer {

}
package secuityConfig;

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

public class SecurityIntializer extends AbstractSecurityWebApplicationInitializer {

}
This is the classes and in the photo that is my project structure . Can anyone Help
No description
61 Replies
JavaBot
JavaBot6mo 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
dan1st6mo ago
What actually happens?
Danix
DanixOP6mo ago
basically i have started spring security module and trying to start the basic security to my endpoint of the controllers via 2 classes called SecurityConfiguration and SecurityIntializer but when i start the project then i didnot get any default login page for security purpose . I think the security page is automatically added to the project using the @Enable Web Security annoatation right ?
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Danix
DanixOP6mo ago
Right now I am following a tutorial and that guy just got a login page for authentication and authorisation after just adding the two classes for config and initialisation as I mentioned above . So is that thing removed now bcz that video is 3 yrs old !
dan1st
dan1st6mo ago
Can you show your pom.xml? I think the default login page is there only if you use Spring Boot
Danix
DanixOP6mo ago
Danix
DanixOP6mo ago
i am not using spring boot now either i am using spring mvc instead What should be the right ?
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Danix
DanixOP6mo ago
I have already given the pom.xml @dan1st | Daniel
dan1st
dan1st6mo ago
That's Spring 6. They are using Spring 5. Can you try overriding the configure method in your WebSecurityConfigurerAdapter and add .formLogin() there?
Danix
DanixOP6mo ago
Which one ?
dan1st
dan1st6mo ago
Which are available? Btw Spring 5 will be out of Open Source support soon
Danix
DanixOP6mo ago
Hmm so I have to use spring 6 for my upcoming projects ?
dan1st
dan1st6mo ago
I would strongly recommend doing that
Danix
DanixOP6mo ago
package config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@EnableWebSecurity(debug = true)
public class Config extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/public/**").permitAll() // Permit access to public resources
.anyRequest().authenticated() // All other requests must be authenticated
.and()
.formLogin()
.loginPage("/login") // Custom login page URL
.defaultSuccessUrl("/dashboard") // Redirect to this URL after successful login
.permitAll() // Allow everyone to access the login page
.and()
.logout()
.logoutUrl("/logout") // URL to trigger logout
.logoutSuccessUrl("/login?logout") // Redirect to this URL after logout
.permitAll(); // Allow everyone to access the logout URL

// Disable Cross-Site Request Forgery (CSRF) protection for simplicity
http.csrf().disable();
}

// Bean for PasswordEncoder (BCryptPasswordEncoder)
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

}
package config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@EnableWebSecurity(debug = true)
public class Config extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/public/**").permitAll() // Permit access to public resources
.anyRequest().authenticated() // All other requests must be authenticated
.and()
.formLogin()
.loginPage("/login") // Custom login page URL
.defaultSuccessUrl("/dashboard") // Redirect to this URL after successful login
.permitAll() // Allow everyone to access the login page
.and()
.logout()
.logoutUrl("/logout") // URL to trigger logout
.logoutSuccessUrl("/login?logout") // Redirect to this URL after logout
.permitAll(); // Allow everyone to access the logout URL

// Disable Cross-Site Request Forgery (CSRF) protection for simplicity
http.csrf().disable();
}

// Bean for PasswordEncoder (BCryptPasswordEncoder)
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

}
i did it but not get anything its same
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Danix
DanixOP6mo ago
package com.controllers;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;




@Controller
public class login {

@ResponseBody
@GetMapping("/hii")
public String name() {
return"Hii";
}

}
package com.controllers;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;




@Controller
public class login {

@ResponseBody
@GetMapping("/hii")
public String name() {
return"Hii";
}

}
This is my controller for checking is this Ok ? @Sammi[OG]
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Danix
DanixOP6mo ago
Alright waiting for the response
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st6mo ago
it just means what you return is converted to text and sent back to the user If you don't have @ResponseBody, it typically tries to render the response
Danix
DanixOP6mo ago
I have already added the responseBody annotation Then whats the issue why the default login page is not showing after hitting the /hii endpoint
dan1st
dan1st6mo ago
@ResponseBody won't show any login page
Danix
DanixOP6mo ago
package com.controllers;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;




@Controller
public class login {


@GetMapping("/hii")
public String name() {
return"Hi";
}

}
package com.controllers;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;




@Controller
public class login {


@GetMapping("/hii")
public String name() {
return"Hi";
}

}
ok now i have removed it and also added a hi.jsp to the retrun but still i can access the jps page or u can say /hii endpoint
dan1st
dan1st6mo ago
Is your security configuration attempting to prevent that in any way?
Danix
DanixOP6mo ago
Wdym ?
dan1st
dan1st6mo ago
Why should it not be accessible?
Danix
DanixOP6mo ago
package config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@EnableWebSecurity(debug = true)
public class Config extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated() // Require authentication for all requests
.and()
.formLogin() // Enable form-based authentication
.loginPage("/login") // Specify the custom login page URL
.permitAll() // Allow everyone to see the login page
.and()
.logout() // Enable logout functionality
.permitAll();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@EnableWebSecurity(debug = true)
public class Config extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated() // Require authentication for all requests
.and()
.formLogin() // Enable form-based authentication
.loginPage("/login") // Specify the custom login page URL
.permitAll() // Allow everyone to see the login page
.and()
.logout() // Enable logout functionality
.permitAll();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

This is what I did
dan1st
dan1st6mo ago
You called your package config? Can you show your beans.xml?
Danix
DanixOP6mo ago
Yehh Beans.xml ?
dan1st
dan1st6mo ago
Do you have such a file? Spring doesn't scan all packages, it only scans package under some root package
Danix
DanixOP6mo ago
No I have web.xml
dan1st
dan1st6mo ago
and if something isn't in that root package, Spring won't find it So Spring doesn't know anything about your Config class
Danix
DanixOP6mo ago
I think no !
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">

<context:component-scan base-package="com.controllers"></context:component-scan>

<bean id = "viewresolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/lib/view/"></property>
<property name="suffix" value=".jsp"></property>

</bean>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">

<context:component-scan base-package="com.controllers"></context:component-scan>

<bean id = "viewresolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/lib/view/"></property>
<property name="suffix" value=".jsp"></property>

</bean>

</beans>
dan1st
dan1st6mo ago
that was a typo I wanted to write So instead of Do this wasn't a question you see the base-package="com.controllers"? That means Spring will only find stuff in com.controllers
Danix
DanixOP6mo ago
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">

<context:component-scan base-package="com.controllers , config"></context:component-scan>

<bean id = "viewresolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/lib/view/"></property>
<property name="suffix" value=".jsp"></property>

</bean>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">

<context:component-scan base-package="com.controllers , config"></context:component-scan>

<bean id = "viewresolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/lib/view/"></property>
<property name="suffix" value=".jsp"></property>

</bean>

</beans>
is this ok
dan1st
dan1st6mo ago
Why don't you just create one common package and put all your application classes in subpackages?
Danix
DanixOP6mo ago
but the issue is still same
dan1st
dan1st6mo ago
no if your base package is com.yourapplication and you have classes in °com.yourapplication.controllers and com.yourapplication.config`, these should be detected
Danix
DanixOP6mo ago
Package are not scanning
No description
dan1st
dan1st6mo ago
you need to set it to com.application not com.application.*
Danix
DanixOP6mo ago
Still 404 error
0:23:10.446 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/LoginPage/", parameters={}
00:23:10.464 [http-nio-8080-exec-1] WARN org.springframework.web.servlet.PageNotFound - No mapping for GET /LoginPage/
00:23:10.467 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND
0:23:10.446 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/LoginPage/", parameters={}
00:23:10.464 [http-nio-8080-exec-1] WARN org.springframework.web.servlet.PageNotFound - No mapping for GET /LoginPage/
00:23:10.467 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND
dan1st
dan1st6mo ago
Do you have a handler for that?
Danix
DanixOP6mo ago
Yess /hii
dan1st
dan1st6mo ago
And you are accessing /hii in your browser?
Danix
DanixOP6mo ago
dan1st
dan1st6mo ago
How are you running the application? Do you have a servers view in Eclipse?
Danix
DanixOP6mo ago
No description
Danix
DanixOP6mo ago
like this ? when i write this in scan com.applicationcom.controllers then it works but not with com.applicationcom
dan1st
dan1st6mo ago
com.application not com.applicationcom but ig if it works, it's fine
Danix
DanixOP6mo ago
i did the same project on spring boot and it runs perfectly @dan1st | Daniel some one said to me that WebsecurityConfigurerAdapter is deprecated… is deprecated is that real ?
dan1st
dan1st6mo ago
It is in Spring 6
Danix
DanixOP6mo ago
So the concepts are same or just changed ?
JavaBot
JavaBot6mo 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.
Tomasm21
Tomasm213mo ago
What did I said wrong?
dan1st
dan1st3mo ago
?
Tomasm21
Tomasm213mo ago
He blocked me or removed from friends. I want to get him back. tried to search from history.
dan1st
dan1st3mo ago
and you think pinging them would result in a notification? Anyways, please don't try to contact people that blocked you via this Discord.
Tomasm21
Tomasm213mo ago
ok. Lock this question.
JavaBot
JavaBot2mo 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.
💤 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.
Want results from more Discord servers?
Add your server