You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/manual/src/docs/asciidoc/_includes/servlet/appendix/faq.adoc
+95-4Lines changed: 95 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -196,7 +196,9 @@ This will be different in different companies, so you have to find it out yourse
196
196
Before adding a Spring Security LDAP configuration to an application, it's a good idea to write a simple test using standard Java LDAP code (without Spring Security involved), and make sure you can get that to work first.
197
197
For example, to authenticate a user, you could use the following code:
198
198
199
-
[source,java]
199
+
====
200
+
.Java
201
+
[source,java,role="primary"]
200
202
----
201
203
202
204
@Test
@@ -214,6 +216,22 @@ public void ldapAuthenticationIsSuccessful() throws Exception {
Session management issues are a common source of forum questions.
@@ -498,7 +516,9 @@ To load the data from an alternative source, you must be using an explicitly dec
498
516
You can't use the namespace.
499
517
You would then implement `FilterInvocationSecurityMetadataSource` to load the data as you please for a particular `FilterInvocation` footnote:[The `FilterInvocation` object contains the `HttpServletRequest`, so you can obtain the URL or any other relevant information on which to base your decision on what the list of returned attributes will contain.]. A very basic outline would look something like this:
500
518
501
-
[source,java]
519
+
====
520
+
.Java
521
+
[source,java,role="primary"]
502
522
----
503
523
504
524
public class MyFilterSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
@@ -526,6 +546,31 @@ You would then implement `FilterInvocationSecurityMetadataSource` to load the da
526
546
527
547
----
528
548
549
+
.Kotlin
550
+
[source,kotlin,role="secondary"]
551
+
----
552
+
class MyFilterSecurityMetadataSource : FilterInvocationSecurityMetadataSource {
553
+
override fun getAttributes(securedObject: Any): List<ConfigAttribute> {
554
+
val fi = securedObject as FilterInvocation
555
+
val url = fi.requestUrl
556
+
val httpMethod = fi.request.method
557
+
558
+
// Lookup your database (or other source) using this information and populate the
559
+
// list of attributes
560
+
return ArrayList()
561
+
}
562
+
563
+
override fun getAllConfigAttributes(): Collection<ConfigAttribute>? {
For more information, look at the code for `DefaultFilterInvocationSecurityMetadataSource`.
530
575
531
576
@@ -537,7 +582,9 @@ The `DefaultLdapAuthoritiesPopulator` loads the user authorities from the LDAP d
537
582
538
583
To use JDBC instead, you can implement the interface yourself, using whatever SQL is appropriate for your schema:
539
584
540
-
[source,java]
585
+
====
586
+
.Java
587
+
[source,java,role="primary"]
541
588
----
542
589
543
590
public class MyAuthoritiesPopulator implements LdapAuthoritiesPopulator {
@@ -562,6 +609,28 @@ To use JDBC instead, you can implement the interface yourself, using whatever SQ
562
609
563
610
----
564
611
612
+
.Kotlin
613
+
[source,kotlin,role="secondary"]
614
+
----
615
+
class MyAuthoritiesPopulator : LdapAuthoritiesPopulator {
616
+
@Autowired
617
+
lateinit var template: JdbcTemplate
618
+
619
+
override fun getGrantedAuthorities(userData: DirContextOperations, username: String): MutableList<GrantedAuthority?> {
620
+
return template.query("select role from roles where username = ?",
621
+
arrayOf(username)
622
+
) { rs, _ ->
623
+
/**
624
+
* We're assuming here that you're using the standard convention of using the role
625
+
* prefix "ROLE_" to mark attributes which are supported by Spring Security's RoleVoter.
626
+
*/
627
+
SimpleGrantedAuthority("ROLE_" + rs.getString(1))
628
+
}
629
+
}
630
+
}
631
+
----
632
+
====
633
+
565
634
You would then add a bean of this type to your application context and inject it into the `LdapAuthenticationProvider`. This is covered in the section on configuring LDAP using explicit Spring beans in the LDAP chapter of the reference manual.
566
635
Note that you can't use the namespace for configuration in this case.
567
636
You should also consult the Javadoc for the relevant classes and interfaces.
@@ -578,7 +647,9 @@ More information can be found in the https://docs.spring.io/spring/docs/3.0.x/sp
578
647
Normally, you would add the functionality you require to the `postProcessBeforeInitialization` method of `BeanPostProcessor`. Let's say that you want to customize the `AuthenticationDetailsSource` used by the `UsernamePasswordAuthenticationFilter`, (created by the `form-login` element). You want to extract a particular header called `CUSTOM_HEADER` from the request and make use of it while authenticating the user.
579
648
The processor class would look like this:
580
649
581
-
[source,java]
650
+
====
651
+
.Java
652
+
[source,java,role="primary"]
582
653
----
583
654
584
655
public class CustomBeanPostProcessor implements BeanPostProcessor {
@@ -603,5 +674,25 @@ public class CustomBeanPostProcessor implements BeanPostProcessor {
603
674
604
675
----
605
676
677
+
.Kotlin
678
+
[source,kotlin,role="secondary"]
679
+
----
680
+
class CustomBeanPostProcessor : BeanPostProcessor {
681
+
override fun postProcessAfterInitialization(bean: Any, name: String): Any {
682
+
if (bean is UsernamePasswordAuthenticationFilter) {
0 commit comments