日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jetty 添加basic auth

發(fā)布時間:2025/3/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jetty 添加basic auth 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

出于安全原因,給jetty加了一個簡單的http basic授權,防止未授權調用,本身服務也是內部調用的,但還是加上了,方式有好幾種,我都試了一遍,記錄一下

第一種 修改web.xml + java 代碼 + 配置文件

加web.xml里面加上這么一段

<security-constraint><web-resource-collection><web-resource-name>api</web-resource-name><url-pattern>/*</url-pattern></web-resource-collection><auth-constraint><role-name>admin</role-name></auth-constraint></security-constraint><login-config><auth-method>BASIC</auth-method><realm-name>Realm</realm-name></login-config><security-role><role-name>admin</role-name></security-role>

大概意思就是啟用basic驗證http訪問權限,需要admin角色,url匹配/*,也就是全部,如果有需要這里可以配置多個角色,然后再根據(jù)url來分開多個角色對應不同的訪問權限,我這里沒這需求,直接攔截所有的

光上面這段還不行,我的jetty是內嵌使用的,所以直接可以在java代碼里面修改配置

ConstraintSecurityHandler securityHandler = (ConstraintSecurityHandler) webapp.getSecurityHandler();HashLoginService loginService = new HashLoginService("Realm", "/etc/realm.properties");loginService.setHotReload(true);BasicAuthenticator basicAuthenticator = new BasicAuthenticator();securityHandler.setRealmName("Realm");securityHandler.setAuthenticator(basicAuthenticator);securityHandler.setLoginService(loginService);

realm.properties

admin: pwd123,admin

格式:用戶名: 密碼, 角色

第二種 修改 java 代碼 + 配置文件

新版的spring mvc可以通過注解,或者代碼的方式啟動一個web項目,而不需要web.xml了,所以上面的方式就不能用了,那么只需要把web.xml里面的代碼以代碼的形式添加到jetty里面就可以了

如下

ConstraintSecurityHandler securityHandler = (ConstraintSecurityHandler) webapp.getSecurityHandler();HashLoginService loginService = new HashLoginService("Realm", "/etc/realm.properties");loginService.setHotReload(true);BasicAuthenticator basicAuthenticator = new BasicAuthenticator();securityHandler.setRealmName("Realm");securityHandler.setAuthenticator(basicAuthenticator);// 多了這一段,意思的web.xml里面的一樣ConstraintMapping constraintMapping = new ConstraintMapping();Constraint constraint = new Constraint("api", "admin");constraint.setAuthenticate(true);constraintMapping.setConstraint(constraint);constraintMapping.getConstraint().setName("api");constraintMapping.getConstraint().setRoles(new String[]{"admin","admin"});constraintMapping.setPathSpec("/*");securityHandler.addConstraintMapping(constraintMapping);securityHandler.setLoginService(loginService);

realm.properties 不變

第三種 只修改 java 代碼

這種方式把配置文件的去掉了,直接在java代碼里面設置用戶名,角色信息,更簡單粗暴,但是不推薦,僅供學習研究

代碼如下

ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();HashLoginService loginService = new HashLoginService("Realm");// 代碼的形式添加用戶名密碼final String role = "admin";final String username = "dev";final String password = "dev";IdentityService identityService = loginService.getIdentityService();UserIdentity admin = identityService.newUserIdentity(new Subject(), new MappedLoginService.KnownUser(username, Credential.getCredential(password)), new String[]{role});loginService.getUsers().put(username, admin);BasicAuthenticator basicAuthenticator = new BasicAuthenticator();securityHandler.setRealmName("Realm");securityHandler.setAuthenticator(basicAuthenticator);ConstraintMapping constraintMapping = new ConstraintMapping();Constraint constraint = new Constraint("api", role);constraint.setAuthenticate(true);constraintMapping.setConstraint(constraint);constraintMapping.getConstraint().setName("api");constraintMapping.getConstraint().setRoles(new String[]{role});constraintMapping.setPathSpec("/*");securityHandler.addConstraintMapping(constraintMapping);securityHandler.setLoginService(loginService);springMvcHandler.setSecurityHandler(securityHandler);

OK,大概就是這樣,三種方式都在這里,有時間我會把代碼上傳到github上面,方便大家查看

轉載于:https://my.oschina.net/362228416/blog/879831

總結

以上是生活随笔為你收集整理的jetty 添加basic auth的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。