<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: CAP /spring authentication with authorisation in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/cap-spring-authentication-with-authorisation/qaa-p/12421357#M4650184</link>
    <description>&lt;P&gt;Please consider using CAPs declarative role-based authorization, as described here: &lt;A href="https://cap.cloud.sap/docs/java/security#role-based-auth" target="test_blank"&gt;https://cap.cloud.sap/docs/java/security#role-based-auth&lt;/A&gt; and here: &lt;A href="https://cap.cloud.sap/docs/guides/authorization#requires" target="test_blank"&gt;https://cap.cloud.sap/docs/guides/authorization#requires&lt;/A&gt;&lt;/P&gt;&lt;P&gt;In Java this will free you from defining your own Spring Security Config, as CAP Java is able to auto-configure everything for you based on the CDS model.&lt;/P&gt;&lt;P&gt;For more details refer to the full CAP Java Security guide: &lt;A href="https://cap.cloud.sap/docs/java/" target="test_blank"&gt;https://cap.cloud.sap/docs/java/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;If you still decide to use your own custom security configuration you can take a look here, maybe this helps: &lt;A href="https://cap.cloud.sap/docs/java/security#custom-spring-security-config" target="test_blank"&gt;https://cap.cloud.sap/docs/java/security#custom-spring-security-config&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 02 Jul 2021 11:06:20 GMT</pubDate>
    <dc:creator>marcbecker</dc:creator>
    <dc:date>2021-07-02T11:06:20Z</dc:date>
    <item>
      <title>CAP /spring authentication with authorisation</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-spring-authentication-with-authorisation/qaq-p/12421356</link>
      <description>&lt;P&gt;I want my application users to be authenticated as well as authorised. The authentication works fine. but i want only users from a group (PMG_USER) to be allowed.&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;i want user abc@sap.com to access api only if he is in PMG_USER group&lt;/STRONG&gt;&lt;/P&gt;
  &lt;P&gt;However, If I keep the restriction only limited to authenticated user, then it works fine. So somehow, custom user role permissions are not given access no matter what I try.&lt;/P&gt;
  &lt;P&gt;The problem is that even though the User has the authority ‘display’ assigned to them, they are not being allowed to access the /client api and instead gives a 403 Forbidden as below.&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1954509-22.png" /&gt;&lt;/P&gt;
  &lt;UL&gt;
   &lt;LI&gt;Here is the code:&lt;/LI&gt;
  &lt;/UL&gt;
  &lt;P&gt;xs-security.json&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;{
    "xsappname": "pmgservice",
    "tenant-mode": "dedicated",
    "description": "Security profile of called application",
    "scopes": [
        {
            "name": "$XSAPPNAME.Display",
            "description": "display"
        }
    ],
    "role-templates": [
        {
            "name": "Viewer",
            "description": "To view items",
            "scope-references": [
                "$XSAPPNAME.Display"
            ]
        }
    ],
    "oauth2-configuration": {
        "redirect-uris": [
            "https://*.applicationstudio.cloud.sap/**"
            "https://*.eu10.hana.ondemand.com/**"
        ]
    }    
}
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;OL&gt; 
  &lt;/OL&gt;
  &lt;P&gt;WebSecurityConfigAdapter.java&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt; @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and() 
                .authorizeRequests()
                .antMatchers("/*").authenticated()
                .antMatchers("/*").hasAnyAuthority("Display")
                .anyRequest().denyAll()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS) // session is created by approuter
                .and().csrf().disable()
                .oauth2ResourceServer()
                .jwt()
                .jwtAuthenticationConverter(getJwtAuthenticationConverter())
                ;
                
    }
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;OL&gt; 
  &lt;/OL&gt;
  &lt;P&gt;Cds file exposing a service&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;@path : 'pmg.svc'
@cds.query.limit.default: 20
@cds.query.limit.max: 100
service PMGService {
    entity client as projection on pmg.client;
}&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;OL&gt; 
  &lt;/OL&gt;
  &lt;P&gt;Cockpit Role Collections : Viewer Role has Display authority and the user is mapped to this Role.&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1954508-ew.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jul 2021 10:27:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-spring-authentication-with-authorisation/qaq-p/12421356</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2021-07-02T10:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: CAP /spring authentication with authorisation</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-spring-authentication-with-authorisation/qaa-p/12421357#M4650184</link>
      <description>&lt;P&gt;Please consider using CAPs declarative role-based authorization, as described here: &lt;A href="https://cap.cloud.sap/docs/java/security#role-based-auth" target="test_blank"&gt;https://cap.cloud.sap/docs/java/security#role-based-auth&lt;/A&gt; and here: &lt;A href="https://cap.cloud.sap/docs/guides/authorization#requires" target="test_blank"&gt;https://cap.cloud.sap/docs/guides/authorization#requires&lt;/A&gt;&lt;/P&gt;&lt;P&gt;In Java this will free you from defining your own Spring Security Config, as CAP Java is able to auto-configure everything for you based on the CDS model.&lt;/P&gt;&lt;P&gt;For more details refer to the full CAP Java Security guide: &lt;A href="https://cap.cloud.sap/docs/java/" target="test_blank"&gt;https://cap.cloud.sap/docs/java/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;If you still decide to use your own custom security configuration you can take a look here, maybe this helps: &lt;A href="https://cap.cloud.sap/docs/java/security#custom-spring-security-config" target="test_blank"&gt;https://cap.cloud.sap/docs/java/security#custom-spring-security-config&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jul 2021 11:06:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-spring-authentication-with-authorisation/qaa-p/12421357#M4650184</guid>
      <dc:creator>marcbecker</dc:creator>
      <dc:date>2021-07-02T11:06:20Z</dc:date>
    </item>
  </channel>
</rss>

