InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
State the difference between @Secured and @RolesAllowed. |
|
Answer» @RolesAllowed: It is a Java standard annotation (JSR250) (i.e., not only spring security). Because this annotation only supports role-based security, it is more limited than the @PreAuthorize annotation. To enable the @RolwesAllowed annotation in your code, add the following line to spring-security.xml and spring boot. XML: <global-method-security jsr250-annotations="enabled"/> Spring boot: @EnableGlobalMethodSecurity(jsr250Enabled = true)@Secured: It is a Spring specific annotation. There is more to it than just role-based security. It secures methods implemented by beans (objects whose life-cycle is managed by the Spring IoC). HOWEVER, Spring Expression Language (SpEL) is not supported for defining security constraints. To enable the @Secured annotation in your code, add the following line to spring-security.xml and spring boot. XML: global-method-security secured-annotations="enabled"/> Spring boot: @EnableGlobalMethodSecurity(securedEnabled=true) ConclusionSpring Security is ONE of the most popular, powerful, and highly customizable access-control frameworks (security framework) that provide authentication, authorization, and other security features for enterprise applications. In this article, we have compiled a comprehensive LIST of Spring Security Interview questions, which are typically asked during interviews. In addition to checking your existing Spring Security skills, these questions serve as a good resource for reviewing some important concepts before you appear for an interview. It is suitable for both freshers as well as EXPERIENCED developers and tech LEADS. Additional Useful Resources Interview questions on Java Interview questions on Spring Boot Spring MVC vs Spring Boot Spring vs Spring Boot |
|
| 2. |
State the difference between @PreAuthorize and @Secured in Spring security. |
||||||||||
|
Answer» A variety of security options are available with SPRING Framework. This framework offers many useful tools or methods for securing applications. In order to PROVIDE method-level security, @Secured and @PreAuthorize are the most commonly used annotations. Compared to @Secured, @PreAuthorize is quite new but becoming well known very fast. There aren't many differences between @Secured and @PreAuthorize; they're nearly identical. However, @PreAuthorize is CONSIDERABLY more powerful than @Secured.
|
|||||||||||
| 3. |
State the difference between ROLE_USER and ROLE_ANONYMOUS in a spring intercept-url configuration. |
Answer»
|
|
| 4. |
Does order matter in the intercept-url pattern? If yes, then in which order should we write it? |
|
Answer» YES, ordering is crucial when we have multiple intercept-URL PATTERNS. Multiple intercept URLS should be WRITTEN from more specific to less specific. As intercept-URL patterns are processed in the order they appear in a SPRING security configuration file, the URL must match the right pattern. |
|
| 5. |
What is the intercept-url pattern and why do we need it? |
|
Answer» <Intercept-url> is used to configure authorizations or access-controls in a Spring Security application. It is used to RESTRICT access to a particular URL. The majority of WEB applications using Spring Security usually have just a few intercept-URLs because their security needs are quite less. Example: Basic Spring security using intercept URL <http realm="Example" use-expressions="false"> <intercept-url PATTERN="/index.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/> <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/> <intercept-url pattern="/admin/*" access="ROLE_ADMIN"/> <intercept-url pattern="/trade/*" access="ROLE_TRADER"/> <intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN,ROLE_TRADER"/> <http-basic/>In this case, index.jsp and admin.jsp can be accessed without authentication. ANYTHING with admin in the URL requires ROLE_ADMIN access, and anything with trade in the URL requires ROLE_TRADER access. |
|
| 6. |
Can you explain what is FilterChainProxy in spring security? |
|
Answer» FilterChainProxy is ANOTHER servlet FILTER designed to invoke the APPROPRIATE filters based on the path of the INCOMING request. It contains INFORMATION about the security filters that make up the security filter chain. It is not directly executed, but it is started by the DelegatingFilterProxy. |
|
| 7. |
Can you explain what is DelegatingFilterProxy in spring security? |
|
Answer» A servlet filter must be declared in the WEB.xml file so that it can be invoked before the REQUEST is passed on to the actual Servlet class. DelegatingFilterProxy is a servlet filter embedded in the spring context. It ACTS as a bridge between web.xml (web application) and the application context (Spring IoC Container). DelegatingFilterProxy is a proxy that delegates an INCOMING request to a group of filters (which are not managed as spring beans) provided by the Spring web framework. It provides full access to the Spring context's life cycle machinery and dependency injection. Whenever a request reaches the web application, the proxy ensures that the request is delegated to Spring Security, and, if everything goes smoothly, it will ensure that the request is DIRECTED to the right resource within the web application. The following example demonstrates how to configure the DelegatingProxyFilter in web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
|
| 8. |
What do you mean by principal in Spring security? |
|
Answer» The principal is actually the CURRENTLY logged in user that is using the APPLICATION. Information/data about the principal (currently authenticated user) is stored in the SecurityContext of the application. As a helper class, SecurityContextHolder provides access to the security context. By default, it uses a THREADLOCAL object to store SecurityContext, so SecurityContext is always ACCESSIBLE to methods in the same thread of execution, EVEN if SecurityContext isn't passed around explicitly. |
|
| 9. |
Name some predefined filters used in spring security and write their functions. |
|
Answer» FILTER chains in Spring Security are very complex and flexible. They use SERVICES such as UserDetailsService and AuthenticationManager to accomplish their tasks. It is also important to consider their orders since you might want to verify their AUTHENTICITY before authorizing them. A few of the important security filters from Spring's filter chain are listed below in the order they occur:
|
|
| 10. |
Explain how the security filter chain works. |
|
Answer» Here's how filters work in a web application:
|
|
| 11. |
What is Spring Security Filter Chain? |
|
Answer» Spring Security executes most of its security features using the filter chain. Spring security is driven through SERVLET filters in web applications. A servlet filter intercepts requests before they REACH the PROTECTED resource (e.g., a Spring controller). As a result, every request for a protected resource will be processed through a spring security filter chain for completing authentication and AUTHORIZATION PURPOSES. |
|
| 12. |
What is JWT? |
|
Answer» JWT (JSON Web Tokens) are tokens that are generated by a server upon user authentication in a web application and are then sent to the CLIENT (normally a browser). As a result, these tokens are sent on every HTTP request, allowing the server to verify or authenticate the user's identity. This method is used for authorizing transactions or requests between client and server. The use of JWT does not intend to hide data, but rather ensure its authenticity. JWTs are signed and encoded, INSTEAD of encrypted. A cryptographic algorithm is used to digitally sign JWTs in order to ensure that they cannot be altered after they are issued. INFORMATION contained in the token is signed by the server's private key in order to ensure integrity.
Three parts make up JSON Web Tokens, separated by a dot (.). The first two (the header and the PAYLOAD) contain Base64-URL encoded JSON, while the third is a cryptographic signature. For example: eyJhbGciOfefeiI1NiJ9.eyJuYW1lIjdgdfeENvZGVyIn0.5dlp7GmziL2dfecegse4mtaqv0_xX4oFUuTDh14KuFTake a look at each of the sections: eyJhbGciOfefeiI1NiJ9 #headereyJuYW1lIjdgdfeENvZGVyIn0 #payload5dlp7GmziL2dfecegse4mtaqv0_xX4oFUuTDh14KuF #signature |
|
| 13. |
Explain what is ProviderManager in Spring security. |
|
Answer» The default implementation of AuthenticationManager is PROVIDERMANAGER. It does not HANDLE the AUTHENTICATION request itself, rather delegates the authentication process to a list of configured AuthenticationProviders. Each authenticationprovider in turn is queried to SEE if it can handle the authentication request. |
|
| 14. |
Explain what is AuthenticationManager in Spring security. |
|
Answer» A Spring Security component called AuthenticationManager tells "How authentication will happen". Because the how part of this question depends on which authentication provider we are using for our application, an AuthenticationManager contains references to all the AuthenticationProviders. AuthenticationManager is the strategy INTERFACE for authentication, which has only one METHOD: public interface AuthenticationManager { Authentication authenticate(Authentication authentication) THROWS AuthenticationException; }AuthenticationManagers can PERFORM one of three actions in their authenticate() method:
|
|
| 15. |
Name security annotations that are allowed to use SpEL. |
|
Answer» Some SECURITY ANNOTATIONS that are allowed to use SpEL include:
These PROVIDE expression-based ACCESS control. In Spring Security, @PreAuthorize is one of the most POWERFUL annotations that allows you to use SpEL. But the old @Secured annotation cannot use it, for example you cannot write @Secured("hasRole('ROLEADMIN')"), but you can do @PreAuthorize("hasRole('ROLEADMIN')"). |
|
| 16. |
What is SpEL (Spring Expression Language)? |
|
Answer» Spring Framework 3.0 introduced Expression LANGUAGE/ SpEL. In Spring Expression Language (SpEL), QUERIES and manipulations of object graphs are possible at runtime. You can use it with XML and annotation-based Spring configurations. JSP EL, OGNL, MVEL and JBoss EL are some of the expression languages available, but SpEL provides additional features INCLUDING string template functionality and method invocation. Example: import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; public class WelcomeTest { public static void main(String[] args) { ExpressionParser parser = new SpelExpressionParser(); Expression EXP = parser.parseExpression("'WELCOMEtoSPEL'"); String message = (String) exp.getValue(); System.out.println(message); //OR //System.out.println(parser.parseExpression("'Hello SPEL'").getValue()); } }Output: WELCOMEtoSPEL |
|